Prev: Dynamicly add dropdown lists to a form populated with php/mysql query
Next: MSXML HTTP translates response status code 204 to 1223
From: Jorge on 6 Sep 2009 19:20 On Sep 6, 10:12 pm, abozhilov <fort...(a)gmail.com> wrote: > (...) > alert(Object.prototype.toString.call(bar)); === alert( ({}).toString.call(bar) ); -- Jorge.
From: Garrett Smith on 6 Sep 2009 20:56 abozhilov wrote: > On Sep 6, 10:30 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> > wrote: >> abozhilov wrote: >>> 1. Attach `foo` property to Global Object. assigned to `foo` reference [...] > In this case you are right. But if i use. > Object.prototype.toString. We get internal [[Class]] property. > In my case: > alert(window.foo); > equivalent to: > alert(window.foo.toString()); The |window.alert| method (nonstandard) converts its argument to a string. Following the internal [[ToString]], alert(foo) would have the same outcome as alert(foo.toString()). Identifier |foo|'s |toString| property is resolved up the prototype chain to |Object.prototype.toString|. So essentially, you are correct in the object's toString being called. Garrett -- comp.lang.javascript FAQ: http://jibbering.com/faq/
From: abozhilov on 7 Sep 2009 02:21 On Sep 7, 2:20 am, Jorge <jo...(a)jorgechamorro.com> wrote: > On Sep 6, 10:12 pm, abozhilov <fort...(a)gmail.com> wrote: > > > (...) > > alert(Object.prototype.toString.call(bar)); > > === alert( ({}).toString.call(bar) ); > > -- > Jorge. I don't need this short optimization. I don't need to create new Object. Object.prototype is static object and he been initialized. ({}).toString.call(bar) First of all, you create ({}) here new object. Not assigned reference to any variable. You used directly and that object will be marked to GC, after executing `toString` method. And that object provide you reference to Object.prototype `object`. This is exactly the same like: Object.prototype.toString.call(bar); But in this case, i don't need to initialize new `object`. Regards.
From: Thomas 'PointedEars' Lahn on 7 Sep 2009 05:23 abozhilov wrote: > On Sep 7, 2:20 am, Jorge <jo...(a)jorgechamorro.com> wrote: >> On Sep 6, 10:12 pm, abozhilov <fort...(a)gmail.com> wrote: >>> (...) >>> alert(Object.prototype.toString.call(bar)); >> === alert( ({}).toString.call(bar) ); >> [...] > > I don't need this short optimization. I don't need to create new > Object. Object.prototype is static object and he been initialized. There is no such thing as a "static object", and certainly `Object.prototype' is not an object; it refers to one, but again that object is not in any sense "static". > ({}).toString.call(bar) > > First of all, you create ({}) here new object. Correct. > Not assigned reference to any variable. Correct. Your point being? > You used directly and that object will be marked to GC, after executing >`toString` method. Correct. Your point being? > And that object provide you reference to Object.prototype `object`. No. That object inherits from the object that `Object.prototype' refers to, through its prototype chain. > This is exactly the same like: > Object.prototype.toString.call(bar); No. At least in JavaScript 1.8.1 as of Firefox/Iceweasel 3.5.1, by contrast the Object Initializer creates a new object using the original Object constructor, regardless of the value that `Object' has been assigned. (ES3F is ambiguous here in section 11.1.5, it says "Create a new object as if by the expression `new Object'" without saying whether `Object' means the original value; apparently, the Mozilla.org people thought of it as meaning the original value.) Test case: Object = function() { this.bar = 42; }; /* * in JavaScript 1.8.1: * string representation of the value assigned before */ String(Object) /* in JavaScript 1.8.1: "undefined" */ typeof {}.bar In addition, your approach uses three lookup operations and two calls through the call() wrapper, while Jorge's approach uses only two lookup operations and two calls. It remains to be seen which approach is more memory-efficient, and which one more runtime-efficient; I would assume that Jorge's approach is more runtime-efficient (as more of the work would be done internally), but less memory-efficient (as a new object is created each time) than yours. There is a third approach that tries to get the best of both worlds: Create and initialize the "empty" Object object only once, and reuse it later. > But in this case, i don't need to initialize new `object`. Apparently, if you want to be on the safe(r) side, you do. (What exactly do you mean by "`object`"?) Please trim your quotes, don't quote signatures. PointedEars ___________ ¹ <http://PointedEars.de/es-matrix#features> -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
From: abozhilov on 7 Sep 2009 14:00
On Sep 7, 12:23 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > abozhilov wrote: > > On Sep 7, 2:20 am, Jorge <jo...(a)jorgechamorro.com> wrote: > >> On Sep 6, 10:12 pm, abozhilov <fort...(a)gmail.com> wrote: > >>> (...) > >>> alert(Object.prototype.toString.call(bar)); > >> === alert( ({}).toString.call(bar) ); > >> [...] > No. At least in JavaScript 1.8.1 as of Firefox/Iceweasel 3.5.1, by contrast > the Object Initializer creates a new object using the original Object > constructor, regardless of the value that `Object' has been assigned. (ES3F > is ambiguous here in section 11.1.5, it says "Create a new object as if by > the expression `new Object'" without saying whether `Object' means the > original value; apparently, the Mozilla.org people thought of it as meaning > the original value.) This is usefully example. If i want to create clean `object`, this approach looks save to do it. > In addition, your approach uses three lookup operations and two calls > through the call() wrapper, while Jorge's approach uses only two lookup > operations and two calls. After worked day. When i come back to my home, i thing about your words. And: > (as more of the work would be done internally) Object.prototype.toString.call I want to know how many lookup operation make JavaScript. I thing in this case lookup's is: 5. Object constructor itself is instance from Function object. In ECMA: 15.3.2.1 A prototype property is automatically created for every function, to provide for the possibility that the function will be used as a constructor. My thing about lookup in: Object.prototype.toString.call 1. Get reference to Object constructor `object`. 2. Because prototype property is "Object instance property", from reference of point 1. JavaScript get `prototype` property of `object`, whos point 1 referred. Here we don't have lookup in prototype chain. 3. Point 2 again is reference to `object`. toString function object is defined directly in `object` who referred from point 2. 4. toString itself is instance from Function object. Interesting in this case is `call` method. He is not defined directly in `object` who reffered `toString`. Here JavaScript check and lookup to protototype chain for `call`. 5. call is defined in toString constructor prototype. ({}).toString.call ({}) - This will be return reference to created object from {} 1. toString isn't defined in `object` referred from {}. Lookup in prototype chain. 2. toString is defined in {} constructor prototype. 3. Point 4 from Object.prototype.toString 4. Point 5 from Object.prototype.toString.call > There is a third approach that tries to get the best of both worlds: Create > and initialize the "empty" Object object only once, and reuse it later. Yes, or maybe directly caching reference to: var foo = Object.prototype.toString; > Apparently, if you want to be on the safe(r) side, you do. (What exactly do > you mean by "`object`"?) When i use `object` i don't talk about reference to `object`. I talk about `object` who been referred. > Please trim your quotes, don't quote signatures. Do you like that? With argumentation line and trim quotes. Thanks a lot. |