发表于: 2016-08-12 21:45:48

0 2159


今天完成的事情:看js原生基础

明天计划的事情:继续加强

遇到的问题:

收获:

function Foo() {

    this.value = 42;

}

Foo.prototype = {

    method: function() {}

};


function Bar() {}


// 设置Bar的prototype属性为Foo的实例对象

Bar.prototype = new Foo();

Bar.prototype.foo = 'Hello World';


// 修正Bar.prototype.constructor为Bar本身

Bar.prototype.constructor = Bar;


var test = new Bar() // 创建Bar的一个新实例


// 原型链

test [Bar的实例]

    Bar.prototype [Foo的实例] 

        { foo: 'Hello World' }

        Foo.prototype

            {method: ...};

            Object.prototype

                {toString: ... /* etc. */};

上面的例子中,test 对象从 Bar.prototype 和 Foo.prototype 继承下来;因此,它能访问 Foo 的原型方法 method。同时,它也能够访问那个定义在原型上的 Foo 实例属性 value。需要注意的是 new Bar() 不会创造出一个新的 Foo 实例,而是重复使用它原型上的那个实例;因此,所有的 Bar 实例都会共享相同的 value 属性。



返回列表 返回列表
评论

    分享到