疑惑来源于我做的一个实验,实验过程如下:
> function Foo(){};
> var f1 = new Foo();
> f1.constructor
这里能按期待的输出,也就是 Foo() 函数本身,但是如果我更改Foo.prototype属性,就有点奇怪了
> Foo.prototype = {};
> var f2 = new Foo();
> f2.constructor
这时候输出
function Object() { [ native code] }
我就开始猜想,
instance.constuctor === instance.__proto__.constructor
接着实验,把构造器的prototype指到Function()实例,看看什么情况
> Foo.prototype = new Function();
> var f3 = new Foo();
> f3.constructor
这时候按猜想,f3.__proto__ == Foo.prototype 也即 new Function(); 而
new Function().constructor就是function Function() { [ native code] },实际输出和猜想吻合。
再来点极端的,如果instance.__proto__ = null, 是个什么情况?
> f3.__proto__ = null;
这时候f3不再具备constructor属性!! 而typeof(f3)还是object,可是一个object既然没有constructor!!看起来猜想是正确的,但是这些是为什么呢?希望各位大牛指点,或者我的思路有什么问题没有?
> function Foo(){};
> var f1 = new Foo();
> f1.constructor
这里能按期待的输出,也就是 Foo() 函数本身,但是如果我更改Foo.prototype属性,就有点奇怪了
> Foo.prototype = {};
> var f2 = new Foo();
> f2.constructor
这时候输出
function Object() { [ native code] }
我就开始猜想,
instance.constuctor === instance.__proto__.constructor
接着实验,把构造器的prototype指到Function()实例,看看什么情况
> Foo.prototype = new Function();
> var f3 = new Foo();
> f3.constructor
这时候按猜想,f3.__proto__ == Foo.prototype 也即 new Function(); 而
new Function().constructor就是function Function() { [ native code] },实际输出和猜想吻合。
再来点极端的,如果instance.__proto__ = null, 是个什么情况?
> f3.__proto__ = null;
这时候f3不再具备constructor属性!! 而typeof(f3)还是object,可是一个object既然没有constructor!!看起来猜想是正确的,但是这些是为什么呢?希望各位大牛指点,或者我的思路有什么问题没有?