Javascript 为什么JS中的某些函数有prototype.constructor prop,而其他函数没有’;T这些功能之间有什么区别?
JS中的每个函数构造函数都有一个Javascript 为什么JS中的某些函数有prototype.constructor prop,而其他函数没有’;T这些功能之间有什么区别?,javascript,prototype,prototypal-inheritance,function-constructor,Javascript,Prototype,Prototypal Inheritance,Function Constructor,JS中的每个函数构造函数都有一个prototype.constructor属性。它存储函数的定义: function Rabbit(value) { this.jumps: value; } alert(Rabbit.prototype.constructor); // alerts exactly the definition of the Rabbit function alert(Array.prototype.constructor); // in Chrome it al
prototype.constructor
属性。它存储函数的定义:
function Rabbit(value) {
this.jumps: value;
}
alert(Rabbit.prototype.constructor); // alerts exactly the definition of the Rabbit function
alert(Array.prototype.constructor); // in Chrome it alerts "function Array() { [native code] }"
现在我检查一个简单函数,而不是函数构造函数,它的主体中没有任何this
:
function bar(val) {
alert(val);
}
alert(bar.prototype.constructor); // behavior is absolutely the same: it alerts the definition of bar
现在我检查内置的Array()
函数:
function Rabbit(value) {
this.jumps: value;
}
alert(Rabbit.prototype.constructor); // alerts exactly the definition of the Rabbit function
alert(Array.prototype.constructor); // in Chrome it alerts "function Array() { [native code] }"
现在我想检查一些内置对象的内置函数:
// The error is thrown in console: TypeError: Cannot read property 'constructor' of undefined
alert(Array.prototype.sort.prototype.constructor);
排序
没有原型
。它在哪里?它的构造函数在哪里?如果您自己添加该方法,它将返回您期望的结果:
Array.prototype.remove= function(){
var what, a= arguments, L= a.length, ax;
while(L && this.length){
what= a[--L];
while((ax= this.indexOf(what))!= -1) this.splice(ax, 1);
}
return this;
}
alert(Array.prototype.remove.prototype.constructor);
不可枚举方法不公开其代码uhh。。。第一个例子是语法错误;应该是
this.jumps=值代码>。所有函数都是构造函数,或者可以被称为构造函数,不管它们是否包含对this
的引用。我怀疑sort()
函数没有原型,因为它是本机方法。“它的构造函数在哪里”Array.prototype.sort.constructor
@James Allardice,右,它的构造函数是function(){[native code]}
,现在我明白了。是不是因为在这个内置功能的原型链中没有更高的东西?