Re: [JSMentors] Can I dynamically make a prototype link?
On 20.12.2010 1:16, Lasse Reichstein wrote: On Sun, 19 Dec 2010 22:19:25 +0100, Juriy Zaytsev kan...@gmail.com wrote: On Sun, Dec 19, 2010 at 7:21 AM, Lasse Reichstein reichsteinatw...@gmail.com wrote: Also, it provides a feature that ES5 doesn't yet: the ability to change an object's prototype chain. ES5 brought us Object.getPrototypeOf, but no setPrototypeOf. This was a conscious decision, as far as I know. Mutable [[Prototype]] carries performance overhead. I think that was one of the concerns of some of the TC-39 members. Mutable [[Prototype]] via __proto__ also breaks other things. IIRC, there was a bug in V8 where JSON.stringify({ __proto__:[] }) serialized to []. This wouldn't happen with `Object.setPrototypeOf` but performance would still be an issue, I suppose. It's there now, so performance won't be any worse than today. Personally, I think the cost of a mutable prototype is subsumed in the cost of having mutable objects in the prototype chai. You still have to check the entire prototype chain every time you make a lookup. Can you clarify? You mean an optimization (sort of inline caching?) which can be made on non-rebinding [[Prototype]]? Dmitry. -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
On Mon, Dec 20, 2010 at 9:24 AM, Dmitry A. Soshnikov dmitry.soshni...@gmail.com wrote: On 20.12.2010 1:16, Lasse Reichstein wrote: On Sun, 19 Dec 2010 22:19:25 +0100, Juriy Zaytsev kan...@gmail.com wrote: On Sun, Dec 19, 2010 at 7:21 AM, Lasse Reichstein reichsteinatw...@gmail.com wrote: Also, it provides a feature that ES5 doesn't yet: the ability to change an object's prototype chain. ES5 brought us Object.getPrototypeOf, but no setPrototypeOf. This was a conscious decision, as far as I know. Mutable [[Prototype]] carries performance overhead. I think that was one of the concerns of some of the TC-39 members. Mutable [[Prototype]] via __proto__ also breaks other things. IIRC, there was a bug in V8 where JSON.stringify({ __proto__:[] }) serialized to []. This wouldn't happen with `Object.setPrototypeOf` but performance would still be an issue, I suppose. It's there now, so performance won't be any worse than today. Personally, I think the cost of a mutable prototype is subsumed in the cost of having mutable objects in the prototype chai. You still have to check the entire prototype chain every time you make a lookup. Can you clarify? You mean an optimization (sort of inline caching?) which can be made on non-rebinding [[Prototype]]? Dmitry. So I've been wondering. Other than the occasional abstraction, how often would you actually need __proto__ in production? I mean, it's clear that __proto__ isn't production ready, due to lack of support. But still I've seen a lot of complaining about __proto__. I mean, I know what __proto__ is and does (so please no lectures on it, thanks!) but how often do you really need it? - peter -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
In addition here's Brendan's explanation of issues: https://mail.mozilla.org/pipermail/es-discuss/2010-April/010917.html On 20.12.2010 13:06, Dmitry A. Soshnikov wrote: On 20.12.2010 12:04, Peter van der Zee wrote: On Mon, Dec 20, 2010 at 9:24 AM, Dmitry A. Soshnikov dmitry.soshni...@gmail.com mailto:dmitry.soshni...@gmail.com wrote: On 20.12.2010 1:16, Lasse Reichstein wrote: On Sun, 19 Dec 2010 22:19:25 +0100, Juriy Zaytsev kan...@gmail.com mailto:kan...@gmail.com wrote: On Sun, Dec 19, 2010 at 7:21 AM, Lasse Reichstein reichsteinatw...@gmail.com mailto:reichsteinatw...@gmail.com wrote: Also, it provides a feature that ES5 doesn't yet: the ability to change an object's prototype chain. ES5 brought us Object.getPrototypeOf, but no setPrototypeOf. This was a conscious decision, as far as I know. Mutable [[Prototype]] carries performance overhead. I think that was one of the concerns of some of the TC-39 members. Mutable [[Prototype]] via __proto__ also breaks other things. IIRC, there was a bug in V8 where JSON.stringify({ __proto__:[] }) serialized to []. This wouldn't happen with `Object.setPrototypeOf` but performance would still be an issue, I suppose. It's there now, so performance won't be any worse than today. Personally, I think the cost of a mutable prototype is subsumed in the cost of having mutable objects in the prototype chai. You still have to check the entire prototype chain every time you make a lookup. Can you clarify? You mean an optimization (sort of inline caching?) which can be made on non-rebinding [[Prototype]]? Dmitry. So I've been wondering. Other than the occasional abstraction, how often would you actually need __proto__ in production? I mean, it's clear that __proto__ isn't production ready, due to lack of support. But still I've seen a lot of complaining about __proto__. Really, I can't remember some _very_ needed use-case of exactly changing `__proto__` dynamically. First, yes, I'm analyze it from the theoretical design viewpoint. ECMAScript borrowed __proto__ from Python. It's called __class__ there -- and it's also mutable. Though, I also can't remember cases when I was needed to change the __class__ of an object dynamically at runtime (probably who program Python more than me can provide some really useful cases), but such an ability is (maybe to create some useful factories which may _classify_ objects at runtime or sort of). The use cases related with ECMAScript I can imagine are: - an elegant way to form inheritance (the same way is used e.g. in Lua); - creating the non-polluted hash-tables; - probably some ad-hoc small optimizations (with using for-in without `hasOwnProperty` check); - an ability to solve the problem instance of a constructor check != object of a needed class (simpler, the ability to inject the needed prototype to e.g. and array instance, http://bit.ly/hhLnRi); - anything else? (probably the same factories I supposed for Python, but it's again, mostly theoretical stuff). Actually, var o = Object.create(null) -- is also quite an elegant way to create a non-inheritable object. Performance issues at _creation_ (with checking the circular references) are not so important IMO (if other can manage it, why JS cannot?). The most interesting case of performance penalty is what Lasse has mentioned and which I wanted to clarify -- what exactly does he mean (an inline caching or anything else? Because, yeah, I hear then and there performance issues, performance issues, including the previous mentioning by kangax, but actually I'm afraid they don't completely understand what exactly issues they mention). I mean, I know what __proto__ is and does (so please no lectures on it, thanks!) Did you confuse me with someone? ;) Do I look like I'd give *useless* lectures? but how often do you really need it? Yeah, I think for exactly (cross-) browser environment, the problem (with give us a mutable __proto__) is really imaginary and I don't remember some the very needed cases (except mentioned above, e.g. with sub-classing an Array -- it's the only normal way today). Though, I used it normally and often for inheritance in only SpiderMonkey-based project (patching Thunderbird). Dmitry. -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
Really, I can't remember some _very_ needed use-case of exactly changing `__proto__` dynamically. First, yes, I'm analyze it from the theoretical design viewpoint. ECMAScript borrowed __proto__ from Python. It's called __class__ there -- and it's also mutable. Though, I also can't remember cases when I was needed to change the __class__ of an object dynamically at runtime (probably who program Python more than me can provide some really useful cases), but such an ability is (maybe to create some useful factories which may _classify_ objects at runtime or sort of). Yeah, theoretically it's interesting. I kind of put that under abstraction :) I mean, I know what __proto__ is and does (so please no lectures on it, thanks!) Did you confuse me with someone? ;) Do I look like I'd give *useless* lectures? No! I kind of fired this as a general question to the list, not specifically to you. So please don't take this as a personal comment :) but how often do you really need it? Yeah, I think for exactly (cross-) browser environment, the problem (with give us a mutable __proto__) is really imaginary and I don't remember some the very needed cases (except mentioned above, e.g. with sub-classing an Array -- it's the only normal way today). Though, I used it normally and often for inheritance in only SpiderMonkey-based project (patching Thunderbird). Yeah. Thanks. -peter -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
On Mon, 20 Dec 2010 09:24:12 +0100, Dmitry A. Soshnikov dmitry.soshni...@gmail.com wrote: On 20.12.2010 1:16, Lasse Reichstein wrote: [Mutalbe __proto__] It's there now, so performance won't be any worse than today. Personally, I think the cost of a mutable prototype is subsumed in the cost of having mutable objects in the prototype chai. You still have to check the entire prototype chain every time you make a lookup. Can you clarify? You mean an optimization (sort of inline caching?) which can be made on non-rebinding [[Prototype]]? Not sure I understand the question. What I'm saying is that an optimizing property lookup, e.g., using inline caching, will need to check the prototype chain for changes in any case if the property is inherited. If it's not inherited, but is an own property, you don't need to check the prototype chain in either case. I.e., it makes no difference whether mutable __proto__ exists, you need to check the prototype chain in the same way whether the prototype chain changes or it's just the properties on the objects that do, so I doubt that a settable prototype will change the performance of property lookups. However, if you don't have a mutable [[Prototype]] property, there are a few other optimizations that become possible, e.g., caching instanceof results, since they depend on the shape of the prototype chain only, not the properties of the objects. That might be the performance issue that TC-39 was thinking of. /Lasse -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
On Sun, 19 Dec 2010 08:39:00 +0100, Juriy Zaytsev kan...@gmail.com wrote: __proto__ also appears to be marked as deprecated in current version of corresponding MDN docs. I wouldn't be surprised to see it go soon (if that doesn't break web too much). I'm afraid I don't see it going away any time soon. For one thing, it probably will break some parts of the web (although I'm happy to see that, e.g., JQuery and ext.js don't use it). Also, it provides a feature that ES5 doesn't yet: the ability to change an object's prototype chain. ES5 brought us Object.getPrototypeOf, but no setPrototypeOf. And it's widely supported: Firefox, Opera, Safari and Chrome, and people are used to writing around IE anyway. It's extremely difficult to remove any feature that is already out there and supported by more than one browser. /Lasse -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
On Sun, Dec 19, 2010 at 7:21 AM, Lasse Reichstein reichsteinatw...@gmail.com wrote: On Sun, 19 Dec 2010 08:39:00 +0100, Juriy Zaytsev kan...@gmail.com wrote: __proto__ also appears to be marked as deprecated in current version of corresponding MDN docs. I wouldn't be surprised to see it go soon (if that doesn't break web too much). I'm afraid I don't see it going away any time soon. For one thing, it probably will break some parts of the web (although I'm happy to see that, e.g., JQuery and ext.js don't use it). Most definitely will break the web. Question is — how badly. __proto__ is non-standard, and it's been repeated hundreds of time — as part of web scripting best practices — not to rely on non-standard features (and prefer standard ones over them). But of course that's not how the web rolls :) Also, it provides a feature that ES5 doesn't yet: the ability to change an object's prototype chain. ES5 brought us Object.getPrototypeOf, but no setPrototypeOf. This was a conscious decision, as far as I know. Mutable [[Prototype]] carries performance overhead. I think that was one of the concerns of some of the TC-39 members. Mutable [[Prototype]] via __proto__ also breaks other things. IIRC, there was a bug in V8 where JSON.stringify({ __proto__: [] }) serialized to []. This wouldn't happen with `Object.setPrototypeOf` but performance would still be an issue, I suppose. And it's widely supported: Firefox, Opera, Safari and Chrome, and people are used to writing around IE anyway. It's extremely difficult to remove any feature that is already out there and supported by more than one browser. Indeed. Well, Mozilla has already deprecated magic __count__, then removed it from FF4. I can imagine them removing __proto__ (or issuing a warning on access/assignment) under strict mode. Other implementations could be more problematic. Is Brendan on this list? [...] -- kangax -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
Juriy Zaytsev wrote: Is Brendan on this list? Unfortunately he is not at the list. I've sent invitation to him, be he hasn't replied yet. I hope he would join at the list. Also Douglas Crockford would accept and his invitation. -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
On Sun, 19 Dec 2010 22:19:25 +0100, Juriy Zaytsev kan...@gmail.com wrote: On Sun, Dec 19, 2010 at 7:21 AM, Lasse Reichstein reichsteinatw...@gmail.com wrote: Also, it provides a feature that ES5 doesn't yet: the ability to change an object's prototype chain. ES5 brought us Object.getPrototypeOf, but no setPrototypeOf. This was a conscious decision, as far as I know. Mutable [[Prototype]] carries performance overhead. I think that was one of the concerns of some of the TC-39 members. Mutable [[Prototype]] via __proto__ also breaks other things. IIRC, there was a bug in V8 where JSON.stringify({ __proto__:[] }) serialized to []. This wouldn't happen with `Object.setPrototypeOf` but performance would still be an issue, I suppose. It's there now, so performance won't be any worse than today. Personally, I think the cost of a mutable prototype is subsumed in the cost of having mutable objects in the prototype chai. You still have to check the entire prototype chain every time you make a lookup. (The V8 bug sounds like just a bug, using instanceof to check for being an array instead of checking [[Class]] - but that's guessing, I don't remember that bug.) I would love to see __proto__ go, if only to not have to special-case the __proto__ property on every object - and not have any properties that exist implicitly on all objects. I doubt it will happen until there are suitable replacements. Even if setPrototypeOf isn't really necessary, __proto__ also serves as a substitute for the missing analogues of Object.create, e.g., an Array.create that creates an array object with a given prototype (and ditto for all the other [[Class]] values). ... Well, Mozilla has already deprecated magic __count__, then removed it from FF4. I can imagine them removing __proto__ (or issuing a warning on access/assignment) under strict mode. Other implementations could be more problematic. If anyone will go first in removing it, I think it will be Mozilla. They seem more willing to take chances than the other browser vendors. /Lasse -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
On Fri, Dec 17, 2010 at 8:43 PM, Asen Bozhilov asen.bozhi...@gmail.comwrote: Garrett Smith wrote: Juriy Zaytsev wrote: var beget = (function() { function F(){ }; All good except for the extra empty statement there. AYK, semicolon is not needed after the FD. Yes! Thanks for catching that. I guess this is typo according knowledge of Juriy. Anyway, more important is discussion about setter of `__proto__'. Setter of internal [[Prototype]] is not a good idea. Breandan has pointed out this topic before. At least setter of [[Prototype]] is not very good from performance. [...] __proto__ also appears to be marked as deprecated in current version of corresponding MDN docs. I wouldn't be surprised to see it go soon (if that doesn't break web too much). [...] -- kangax -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
Garrett Smith wrote: Juriy Zaytsev wrote: var beget = (function() { function F(){ }; All good except for the extra empty statement there. AYK, semicolon is not needed after the FD. I guess this is typo according knowledge of Juriy. Anyway, more important is discussion about setter of `__proto__'. Setter of internal [[Prototype]] is not a good idea. Breandan has pointed out this topic before. At least setter of [[Prototype]] is not very good from performance. This setter needs to check for cycling prototype chain. If there is not a finite prototype chain it should throw an error. For example: var x = {}; x.__proto__ = x; This design allows to be created reference cycle, because we have a reference to created object trough the scope chain. In this case it must be checked for reference cycle. But in this case: var x = { __proto__ : someObject }; This is pretty safe to be used in object literal, because when you assign value of `__proto__' you have not got any references to created object via object literal. var x = { __proto__ : x //x is undefined here }; From this point of view, this object literal is similar as `Object.create' method introduced by ECMA-262-5. `Object.create' is factory method and this allows for optimizations. There is not any reasons for checking reference cycle, because it cannot be created at all. `__proto__' has and some side effects in SpiderMonkey, but we have discussed them many times at c.l.js -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com
Re: [JSMentors] Can I dynamically make a prototype link?
On 12/17/10, Asen Bozhilov asen.bozhi...@gmail.com wrote: Garrett Smith wrote: Juriy Zaytsev wrote: var beget = (function() { function F(){ }; All good except for the extra empty statement there. AYK, semicolon is not needed after the FD. (AYK means as you know.) I guess this is typo according knowledge of Juriy. That sort of typo is one I sometimes make when converting FE to FD. e.g. | var f = function(){}; // FunctionExpresssion That's fine, but say I decide to change to FD, | function f(){}; // FunctionDeclaration. Whoops! I've left the semicolon on there. The Empty statement is not needed and should be removed. | function f(){} OK, now it's fixed. I wish my IDE would indicate that useless empty statement, but it doesn't, so sometimes I check in code with unneeded semicolons. Code reviews can catch those though. In turn, possibly kangax' advice to use `var` was given to someone who already knew that, too. But sure, he did the right thing by explaining it either way. But not everybody knows these things, and that'[s what such corrections can help. THe idea is just getting the right information out there for everybody. That's a good thing. -- Garrett -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com