原型链中的Function和Function.prototype对象在哪里?

时间:2017-02-11 20:05:49

标签: javascript

我正在阅读JS中的原型链接。我在网上看到了这个原理图。

enter image description here

我的问题是:

由于函数A和B是类型为Object的对象,在继承树中的某处不应该有Function和Function.prototype对象吗?

1 个答案:

答案 0 :(得分:2)

构造函数没有在原型链中以层次结构图形化,它们构造的对象是图的焦点。

[[proto]](可通过obj.__proto__访问,其中objObject)和Prototype之间的区别存在重要区别(和混淆)物体的属性,即。 obj.prototype

这段代码可以解释:



function A(name){
  this.name = name
  this.getName = function(){ return name }
}

function B(name){
  A.call(this, name)
}

var b = new B('yep')
var anotherB = new B('me too')

console.log(b.getName()) // yep

/* both B & A constructors inherit from Function but B doesn't inherit from A */
console.log(B instanceof Function && A instanceof Function) //true
console.log(B.prototype === A.prototype) //false
console.log(B.__proto__ === A.__proto__) //true
console.log(B instanceof A) //false


/* an instance of B is not an instance of A */
console.log(b instanceof B) //true
console.log(b instanceof A) //false

/* an instance of B has the prototype of B but not the same [[proto]] as B */
console.log(B.prototype.isPrototypeOf(b)) //true
console.log(b.__proto__ === B.__proto__ ) //false

/* instances of B have the same [[proto]] but not the same [[proto]] as B */
console.log(b.__proto__ === anotherB.__proto__ ) //true




function B不会从function A继承。 function Afunction B都继承自Function的{​​{1}}。它们都具有相同的原型链(缩写)。

Objectfunction A - > function A - > Function

Objectfunction B - > function B - > Function

这与他们构建的对象不同。在从Object返回的对象的情况下,原型链是function B - > B - > A

相关问题