js面向对象的实现(example 二)

//这个方法和上篇文章(js面向对象的实现(example 一))中的方法类似,但是更为简洁
    //通过函数赋值的方式来构造对象
    //同样通过闭包的方式来封装对象及内部变量
    (function () {
        function Persion(name) {
            var _this={};
            _this.name=name;
            _this.nick_name="aimi";
            _this.say=function () {
                alert("你好啊,"+_this.name+"-"+_this.nick_name);
            };

            return _this;
        }
        window.Persion=Persion;
    }());


    function Teacher(name) {
        var _this= new Persion(name);
        var psay=_this.say;
        _this.say=function () {
            psay.call(this);
             alert("我很好,"+_this.name);
        };
        return _this;
    }

    var t = new Teacher("大头");
    t.say();

  

posted @ 2016-10-13 09:36  Window2016  阅读(181)  评论(0编辑  收藏  举报