Почему некоторые функции в JS имеют свойство prototype.constructor, а другие нет? В чем разница между этими функциями?

Каждый конструктор функции в JS имеет prototype.constructor имущество. И он хранит определение функции:

function Rabbit(value) {
    this.jumps: value;
}
alert(Rabbit.prototype.constructor);  // alerts exactly the definition of the Rabbit function

Теперь я проверяю простую функцию, а не конструктор функции, она не имеет каких-либо this в теле:

function bar(val) {
    alert(val);
}
alert(bar.prototype.constructor);   // behavior is absolutely the same: it alerts the definition of bar

Сейчас я проверяю встроенный Array() функция:

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);

sort не имеет prototype, Где это находится? И где его конструктор?

1 ответ

Решение

Если вы добавите метод самостоятельно, он вернет то, что вы ожидаете:

    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);

Неперечислимые методы не раскрывают свой код

Другие вопросы по тегам