Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Запутался с прототипами... (https://javascript.ru/forum/misc/60461-zaputalsya-s-prototipami.html)

Keramet 31.12.2015 21:48

Ruslan_xDD,
вот 2 фрагмента, можете объяснить в чём разница будет?
function SomeF() {
  this.name = "";
}
SomeF.prototype.IsNamed = false;


function SomeF() {
  this.name = "";
  this.IsNamed = false;
}

рони 31.12.2015 21:58

Цитата:

Сообщение от Keramet
почему тогда в моём примере выдаёт false

потому что не обьект

ruslan_mart 01.01.2016 08:06

function MyClass() {};
MyClass.prototype.isNamed = false;


var a = new MyClass();
var b = new MyClass();

alert([a.isNamed, b.isNamed]);

MyClass.prototype.isNamed = true;

alert([a.isNamed, b.isNamed]);




function MyClass() {
		this.isNamed = false;
};


var a = new MyClass();
var b = new MyClass();

alert([a.isNamed, b.isNamed]);

MyClass.prototype.isNamed = true;

alert([a.isNamed, b.isNamed]);

Keramet 01.01.2016 17:32

Я разобрался!! :)
Цитата:

Сообщение от Keramet (Сообщение 402124)
Всем привет!
Наследуется ли свойство прототипа всем объектам?
К примеру:

function SomeF() {
  this.name = "";
}
SomeF.prototype.IsNamed = false;
SomeF.prototype.SetName = function(txt) {
   this.name = txt;
   this.IsNamed = true;
}
var a = new SomeF();
alert(a.IsNamed);
a.SetName("AAA");
alert(a.IsNamed);

var b = new SomeF();
alert(b.IsNamed);

Разве не должна строка 15 выдать true?

Для того, чтобы всё работало как надо, строку 07 - this.IsNamed = true; надо заменить на SomeF.prototype.IsNamed = true;
Тогда строка 15 будет выдавать true (что и требовалось). Всем спасибо!


Часовой пояс GMT +3, время: 22:23.