Answer the question
In order to leave comments, you need to log in
JavaScript prototypes [Resolved, everything is so sad]
var Class = function () {};
Class.prototype = {
attr : {}
};
var foo = new Class,
bar = new Class;
foo != bar //Хорошо
foo.attr == bar.attr //o_O
Answer the question
In order to leave comments, you need to log in
Create different prototypes or declare ownProperty
attr
in the constructor otherwise nothing. Logically, foo.attr === bar.attr
because both of them refer toClass.prototype.attr
In general, this is a typical prototype behavior, it is the same for everyone in the chain behind it. Can you explain better what you want?
String.prototype.q = 5;
q = "";
w = "w";
console log(q != w);
console.log(qq == wq);
foo.attr === bar.attr because foo.attr is foo.__proto__.attr and foo.__proto__ === bar.__proto__. When you make the assignment foo.attr = {}, foo.attr is no longer foo.__proto__.attr.
By the way, you have a typical js error with var and several variables separated by commas.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question