I was surprised by the following behaviour:
d8> var a = { foo: "hello" }; d8> var b = Object.create(a); d8> b.foo; hello d8> a.foo; hello d8> b.foo += " world"; hello world d8> b.foo; hello world d8> a.foo; hello
It is surprising because b.foo += " world"; behaves literally like b.foo = b.foo + " world"; and each b.foo refers to a different property (one is on the object a and the other on the object b). I wonder if all other JS VM's do the same.
Marc
Afficher les réponses par date
That's how the JS prototype-based inheritance model is supposed to work. When set a property on b, it will be defined on b, never on its parents, unless you explicitly set the property on the parents. I agree that it's strange, and perhaps undesirable.
I was surprised by the following behaviour:
d8> var a = { foo: "hello" }; d8> var b = Object.create(a); d8> b.foo; hello d8> a.foo; hello d8> b.foo += " world"; hello world d8> b.foo; hello world d8> a.foo; hello
It is surprising because b.foo += " world"; behaves literally like b.foo = b.foo + " world"; and each b.foo refers to a different property (one is on the object a and the other on the object b). I wonder if all other JS VM's do the same.
Marc
Tachyon-list mailing list Tachyon-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/tachyon-list
On 2011-10-08, at 12:43 PM, chevalma@iro.umontreal.ca wrote:
That's how the JS prototype-based inheritance model is supposed to work. When set a property on b, it will be defined on b, never on its parents, unless you explicitly set the property on the parents. I agree that it's strange, and perhaps undesirable.
I'm OK with that behaviour, and I understand it. The problem I see is that X += Y does not mean "find *the* cell where X is located, and add Y to it" (as in C, C++, Java, etc). The JS semantics has 2 lookup operations.
Marc