Re: Setting a property in the prototype chain?

2011-04-12 Thread Lasse Reichstein
On Tue, 12 Apr 2011 07:55:26 +0200, Allen Wirfs-Brock al...@wirfs-brock.com wrote: On Apr 11, 2011, at 6:45 PM, Axel Rauschmayer wrote: var obj={}.valueOf.call(obj); // ToObject in case primitive value passed ... {}.valueOf is the same value as Object.prototype.valueOf assuming none

Re: Setting a property in the prototype chain?

2011-04-12 Thread Axel Rauschmayer
Object.prototype.valueOf as specified is essentially a call to the internal ToObject operation that wrappers primitive values. I put it in to deal with cases like getDefiningObject(some string, match) Oh my, I've just consulted the ECMAScript spec and it's true. But isn't that

Re: Setting a property in the prototype chain?

2011-04-12 Thread Mark S. Miller
On Tue, Apr 12, 2011 at 3:57 AM, Lasse Reichstein reichsteinatw...@gmail.com wrote: On Tue, 12 Apr 2011 07:55:26 +0200, Allen Wirfs-Brock al...@wirfs-brock.com wrote: On Apr 11, 2011, at 6:45 PM, Axel Rauschmayer wrote: var obj={}.valueOf.call(obj); // ToObject in case primitive value

Re: Setting a property in the prototype chain?

2011-04-12 Thread Allen Wirfs-Brock
On Apr 12, 2011, at 12:57 AM, Lasse Reichstein wrote: In that case, why not just obj = Object(obj); It has the added advantage of not returning the global object for null and undefined, but just a plain new object (although either can be said to be wrong in this case). I used

Re: Setting a property in the prototype chain?

2011-04-12 Thread Erik Arvidsson
On Tue, Apr 12, 2011 at 08:41, Allen Wirfs-Brock al...@wirfs-brock.com wrote: In ES5  {}.valueOf.call(undefined) doesn't yield the this object. In general, ES5 built-ins do not convert their this parameter to the global object when it is undefined. FYI

Re: Setting a property in the prototype chain?

2011-04-11 Thread Jorge
On 11/04/2011, at 05:14, Mark S. Miller wrote: On Apr 10, 2011, at 22:18 , David Herman wrote: function getDefiningObject(obj, key) { if (!(key in obj)) throw new Error(key + key + not found); while (!obj.hasOwnProperty(key)) That should be while

When is a JavaScript program correct? (was: Setting a property in the prototype chain?)

2011-04-11 Thread Mark S. Miller
On Sun, Apr 10, 2011 at 11:21 PM, David Herman dher...@mozilla.com wrote: I wondered if someone was going to make this point. That should be while (!{}.hasOwnProperty.call(obj, key)) which works even if obj has an own property named 'hasOwnProperty'. Not if someone mutates

Re: When is a JavaScript program correct? (was: Setting a property in the prototype chain?)

2011-04-11 Thread Dmitry A. Soshnikov
On 11.04.2011 18:07, Mark S. Miller wrote: On Sun, Apr 10, 2011 at 11:21 PM, David Herman dher...@mozilla.com mailto:dher...@mozilla.com wrote: I wondered if someone was going to make this point. That should be while (!{}.hasOwnProperty.call(obj, key))

Re: Setting a property in the prototype chain?

2011-04-11 Thread Allen Wirfs-Brock
Personally, I prefer Object.getOwnPropertyDescriptor as a property existence test because it doesn't have the reflection the meta-circularity concern. I would write Dave's original function as: function getDefiningObject(obj, key) { var obj={}.valueOf.call(obj); // ToObject in case

Re: When is a JavaScript program correct? (was: Setting a property in the prototype chain?)

2011-04-11 Thread David Bruant
Le 10/04/2011 23:27, Dmitry A. Soshnikov a écrit : On 11.04.2011 18:07, Mark S. Miller wrote: On Sun, Apr 10, 2011 at 11:21 PM, David Herman dher...@mozilla.com mailto:dher...@mozilla.com wrote: I wondered if someone was going to make this point. That should be

Re: When is a JavaScript program correct? (was: Setting a property in the prototype chain?)

2011-04-11 Thread Mark S. Miller
On Sun, Apr 10, 2011 at 5:27 PM, Dmitry A. Soshnikov dmitry.soshni...@gmail.com wrote: As I see it, you address the issue of unstratified meta-programming (note, I take the issue in quotes, since there's no a single meaning whether the unstratified meta-level is so bad). It depends on how

Re: When is a JavaScript program correct? (was: Setting a property in the prototype chain?)

2011-04-11 Thread Mike Samuel
2011/4/11 Mark S. Miller erig...@google.com: We would like a notion of correctness that allows us to reason in a modular manner: A correct composition of individually correct components should yield a correct composite, where the correctness of the composition depends only on the contracts of

Re: When is a JavaScript program correct? (was: Setting a property in the prototype chain?)

2011-04-11 Thread Mark S. Miller
On Mon, Apr 11, 2011 at 2:48 PM, Mike Samuel mikesam...@gmail.com wrote: 2011/4/11 Mark S. Miller erig...@google.com: We would like a notion of correctness that allows us to reason in a modular manner: A correct composition of individually correct components should yield a correct

Re: Setting a property in the prototype chain?

2011-04-11 Thread Brendan Eich
On Apr 11, 2011, at 8:58 PM, David Bruant wrote: Le 11/04/2011 21:47, Brendan Eich a écrit : On Apr 11, 2011, at 6:36 PM, Allen Wirfs-Brock wrote: Personally, I prefer Object.getOwnPropertyDescriptor as a property existence test because it doesn't have the reflection the meta-circularity

Re: Setting a property in the prototype chain?

2011-04-11 Thread Allen Wirfs-Brock
On Apr 11, 2011, at 12:47 PM, Brendan Eich wrote: Then the only downside is the pd allocation. Any way to avoid that? /be Use a GC that supports cheap allocation/recovery of short-lived objects :-) It's probably premature optimization to worry about that one pd allocation without

Re: Setting a property in the prototype chain?

2011-04-11 Thread David Bruant
Le 11/04/2011 22:01, Brendan Eich a écrit : On Apr 11, 2011, at 8:58 PM, David Bruant wrote: Le 11/04/2011 21:47, Brendan Eich a écrit : On Apr 11, 2011, at 6:36 PM, Allen Wirfs-Brock wrote: Personally, I prefer Object.getOwnPropertyDescriptor as a property existence test because it

Re: Setting a property in the prototype chain?

2011-04-11 Thread Brendan Eich
On Apr 11, 2011, at 9:21 PM, David Bruant wrote: Actually I was wrong I think. With the introduction of proxies, ES engines won't be able to trivially prevent pd allocation as they could with regular objects just based on static analysis (since static analysis will often fail at saying

Re: When is a JavaScript program correct? (was: Setting a property in the prototype chain?)

2011-04-11 Thread David Herman
Sure, I'll buy that. There's a qualitative difference in how uncooperatively your program is behaving if it mutates primordials to violate their usual contracts, as opposed to simply creating an object that overrides hasOwnProperty, which ought to be allowed. The word theory might be a little

Re: Setting a property in the prototype chain?

2011-04-11 Thread Axel Rauschmayer
var obj={}.valueOf.call(obj); // ToObject in case primitive value passed Can you explain how this works? - Why {} and not Object.prototype? - I know valueOf as a method that returns a primitive if an object can be converted to one and |this|, otherwise. Oddly enough, this works both in

Re: Setting a property in the prototype chain?

2011-04-11 Thread Allen Wirfs-Brock
On Apr 11, 2011, at 6:45 PM, Axel Rauschmayer wrote: var obj={}.valueOf.call(obj); // ToObject in case primitive value passed Can you explain how this works? - Why {} and not Object.prototype? - I know valueOf as a method that returns a primitive if an object can be converted to one

Setting a property in the prototype chain?

2011-04-10 Thread Axel Rauschmayer
As far as I am aware, there is no way to change a property that isn’t at the beginning of the property chain, because the change will create a new property there. Are there plans to change this? It would be nice to have something akin to class methods (without accessing the prototype via some

Re: Setting a property in the prototype chain?

2011-04-10 Thread Dmitry A. Soshnikov
On 10.04.2011 21:24, Axel Rauschmayer wrote: As far as I am aware, there is no way to change a property that isn’t at the beginning of the property chain, because the change will create a new property there. Are there plans to change this? It would be nice to have something akin to class

Re: Setting a property in the prototype chain?

2011-04-10 Thread David Bruant
Le 10/04/2011 19:24, Axel Rauschmayer a écrit : As far as I am aware, there is no way to change a property that isn’t at the beginning of the property chain, because the change will create a new property there. Are there plans to change this? It would be nice to have something akin to

Re: Setting a property in the prototype chain?

2011-04-10 Thread David Herman
function getDefiningObject(obj, key) { if (!(key in obj)) throw new Error(key + key + not found); while (!obj.hasOwnProperty(key)) obj = Object.getPrototypeOf(obj); return obj; } On Apr 10, 2011, at 10:24 AM, Axel Rauschmayer wrote: As far as I am aware, there is

Re: Setting a property in the prototype chain?

2011-04-10 Thread Axel Rauschmayer
Exactly what I was looking for. Thanks. On Apr 10, 2011, at 22:18 , David Herman wrote: function getDefiningObject(obj, key) { if (!(key in obj)) throw new Error(key + key + not found); while (!obj.hasOwnProperty(key)) obj = Object.getPrototypeOf(obj); return obj;

Re: Setting a property in the prototype chain?

2011-04-10 Thread Axel Rauschmayer
As far as I am aware, there is no way to change a property that isn’t at the beginning of the property chain, because the change will create a new property there. Are there plans to change this? It would be nice to have something akin to class methods (without accessing the prototype via

Re: Setting a property in the prototype chain?

2011-04-10 Thread Mark S. Miller
On Sun, Apr 10, 2011 at 6:59 PM, Axel Rauschmayer a...@rauschma.de wrote: Exactly what I was looking for. Thanks. Not exactly... On Apr 10, 2011, at 22:18 , David Herman wrote: function getDefiningObject(obj, key) { if (!(key in obj)) throw new Error(key + key + not

Re: Setting a property in the prototype chain?

2011-04-10 Thread David Herman
I wondered if someone was going to make this point. That should be while (!{}.hasOwnProperty.call(obj, key)) which works even if obj has an own property named 'hasOwnProperty'. Not if someone mutates Object.prototype.hasOwnProperty or Function.prototype.call. I don't think we

Re: Setting a property in the prototype chain?

2011-04-10 Thread Axel Rauschmayer
Exactly what I was looking for. Thanks. Not exactly... True. But it's OK for me as a work-around. That should be while (!{}.hasOwnProperty.call(obj, key)) which works even if obj has an own property named 'hasOwnProperty'. IIRC, {} creates a new instance for each