Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么JS中的某些函数有prototype.constructor prop,而其他函数没有’;T这些功能之间有什么区别?_Javascript_Prototype_Prototypal Inheritance_Function Constructor - Fatal编程技术网

Javascript 为什么JS中的某些函数有prototype.constructor prop,而其他函数没有’;T这些功能之间有什么区别?

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

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 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]}
,现在我明白了。是不是因为在这个内置功能的原型链中没有更高的东西?