Prev: Native Objects .prototype extending
Next: The 3rd way
From: Dmitry A. Soshnikov on 28 Nov 2009 08:10 On Nov 27, 6:14 pm, Darren Satkunas <aluz...(a)gmail.com> wrote: > On Nov 27, 10:05 am, "Dmitry A. Soshnikov" > > > > <dmitry.soshni...(a)gmail.com> wrote: > > On Nov 27, 5:54 pm, Darren Satkunas <aluz...(a)gmail.com> wrote: > > > > I'm looking for a way to create a new instance of a Javascript Object > > > and assign it to an instance name that is determined at runtime. Is > > > there a way to do this without using the dreaded eval() function shown > > > below? > > > > function MyObj(value){ > > > this.value = value;} > > > > //use a static instance name > > > var Obj1 = new MyObj('abc'); > > > console.log(Obj1.value); > > > > //using a dynamic instance name > > > var runtimeInstanceName = 'Obj2'; //assume this string is determined > > > at runtime > > > eval(runtimeInstanceName+" = new MyObj('def');"); > > > console.log(Obj2.value); > > > If base object is known, in general case it looks like: > > > someObject[runtimeInstanceName] = new MyObj('def'); > > > In case of global context, you can use `window` object to manages this > > case (or, if exactly in global context - `this` keyword): > > > window[runtimeInstanceName] = new MyObj('def'); > > console.log(Obj2.value); > > > /ds > > Thanks Dmitry, works like a charm. > But if you need to declare a local variable of some function context, you still have to use `eval`, as there's no ability to access the activation object (which stores declared variable) of the function context. > I didn't realize that `window` could be used as a reference to the > global scope. In general, `window` property would be enough (regardless specific implementation as it's a host object), but actually, as I mentioned above, in the global context, `this` keyword references to the global object. So you can do something like this in the global context: this[runtimeInstanceName] = new MyObj('def'); If you're sure that `window` name isn't hidden by some local variable (e.g. in some function `var window = 10;`), you can normally use it to manage your case.
From: Asen Bozhilov on 28 Nov 2009 08:45 Dmitry A. Soshnikov wrote: > But if you need to declare a local variable of some function context, > you still have to use `eval`, as there's no ability to access the > activation object (which stores declared variable) of the function > context. Dmitry that isn't only approach to declare *local* variable. He cans put in the front of the scope chain `object' which haves property for instance name using `with' statement: function testFn() { with({'instance_name' : new Foo()}) { window.alert(instance_name); } }; `with' statement put on the front of Scope Chain reference to `object' which have property `instance_name'. Resolving properties against Scope Chain here is interesting part. Global Object <- Global execution context ^ AO/VO <- `testFn' ^ Object -> Object.prototype -> null So here first property will be resolved from Prototype chain. If in Prototype chain doesn't exist property with that name, will be *searching* in the next `object' in Scope Chain.
From: Thomas 'PointedEars' Lahn on 28 Nov 2009 09:34 Asen Bozhilov wrote: > Dmitry A. Soshnikov wrote: >> But if you need to declare a local variable of some function context, >> you still have to use `eval`, as there's no ability to access the >> activation object (which stores declared variable) of the function >> context. > > Dmitry that isn't only approach to declare *local* variable. In fact, using eval() is probably the most risky approach to declare a variable, local or otherwise. Have you ever heard of code injection? Not everything that can be done should be done. > He cans put in the front of the scope chain `object' which haves property > for instance name using `with' statement: That is _not_ a declaration, let alone a variable declaration. PointedEars -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: Dmitry A. Soshnikov on 28 Nov 2009 09:41 On Nov 28, 4:45 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: > Dmitry A. Soshnikov wrote: > > But if you need to declare a local variable of some function context, > > you still have to use `eval`, as there's no ability to access the > > activation object (which stores declared variable) of the function > > context. > > Dmitry that isn't only approach to declare *local* variable. He cans > put in the front of the scope chain `object' which haves property for > instance name using `with' statement: > > function testFn() > { > with({'instance_name' : new Foo()}) > { > window.alert(instance_name); > } > > }; > > `with' statement put on the front of Scope Chain reference to `object' > which have property `instance_name'. Resolving properties against > Scope Chain here is interesting part. > > Global Object <- Global execution context > ^ > AO/VO <- `testFn' > ^ > Object -> Object.prototype -> null > > So here first property will be resolved from Prototype chain. If in > Prototype chain doesn't exist property with that name, will be > *searching* in the next `object' in Scope Chain. Yeah, thanks Asen, I sure know about `with` statement affecting on the scope chain. But that isn't variable declaration. Regarding prototype chain analysis, yep this affect also can be seen in some versions of Spidermonkey for named function expressions (NFE), when special object is created by specification "as by new Object()", and also for in some other implementations when activation object inherits from `Object.prototype`. /ds
From: Asen Bozhilov on 28 Nov 2009 09:51 On 28 îÏÅÍ, 16:34, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > That is _not_ a declaration, let alone a variable declaration. Absolutely right. I make little error when i write previous post. In previous post I want to write: Dmitry that isn't only approach to "declare" local variable. But i catch that after submit. Apologize.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Native Objects .prototype extending Next: The 3rd way |