A
A
ArtemSmirnov2011-07-11 10:22:51
JavaScript
ArtemSmirnov, 2011-07-11 10:22:51

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



After a little thought, I realized that it is quite logical that the prototype has a reference to an object, but is there any way to get around this misunderstanding without creating attr directly from the constructor, a huge request, do not offer creation through the constructor and do not write that this is the only solution, because to. if there are no others, then it will be logical.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mikhail Davydov, 2011-07-11
@ArtemSmirnov

Create different prototypes or declare ownProperty attrin the constructor otherwise nothing. Logically, foo.attr === bar.attrbecause both of them refer toClass.prototype.attr

A
Anatoly, 2011-07-11
@taliban

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

H
homm, 2011-07-11
@homm

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.

V
Vyacheslav Plisko, 2011-07-11
@AmdY

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 question

Ask a Question

731 491 924 answers to any question