0
var a = 1;

function seven() {
return this.a = 7;
}
alert(seven.prototype.a);
// [object].prototype.property loses to
// [object].property in the lookup chain. For example...

// Won't get reached, because 'a' is set in the constructor above.
seven.prototype.a = -1;

// Will get reached, even though 'b' is NOT set in the constructor.
seven.prototype.b = 8;

Не могу понять почему

alert(seven.prototype.a);

Не выводит 7?

1 ответ 1

3

prototype не так используется, как-то так надо

function Seven(){};
Seven.prototype.a=7;

var seven =new Seven();
console.log(seven.a);
console.log(Seven.prototype.a);

а у вас вообще this.a не задается, т.к. функция не выполняется, seven.prototype.а не задан.

код для медитации:

function seven() {
  alert("Constructor fired!"); //не запустится, пока не сделаете что-то типа seven();
  return this.a = 7;
}

alert(seven.prototype.a); //undefined

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.