Prev: Native Objects .prototype extending
Next: The 3rd way
From: Darren Satkunas on 27 Nov 2009 09:54 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);
From: Dmitry A. Soshnikov on 27 Nov 2009 10:05 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
From: Darren Satkunas on 27 Nov 2009 10:14 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. I didn't realize that `window` could be used as a reference to the global scope.
From: Asen Bozhilov on 27 Nov 2009 10:15 Darren Satkunas 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? > //using a dynamic instance name > var runtimeInstanceName = 'Obj2'; //assume this string is determined > at runtime > eval(runtimeInstanceName+" = new MyObj('def');"); When been evaluate eval, will be defined property of Global Object. See in ECMA 262-3 11.2.1 Property Accessors and from FAQ <URL: http://www.jibbering.com/faq/#propertyAccessAgain >
From: Thomas 'PointedEars' Lahn on 27 Nov 2009 17:29
Darren Satkunas wrote: > "Dmitry A. Soshnikov" wrote: >> 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); >> [...] > > Thanks Dmitry, works like a charm. It works by coincidence only; it tests positive only because of superficial testing. It would not work so well if `runtimeInstanceName' happened to store the name of a property of this Window instance that was implemented read-only or had a significant meaning (e.g., `innerWidth' in Gecko-based browsers; `window["innerWidth"] = new Object();' just managed to reduce the width of my Iceweasel 3.5.5 browser window to about 20 pixels, while reading it back yielded 474 [pixels]). DO NOT do this. > I didn't realize that `window` could be used as a reference to the > global scope. First of all, you would not be referencing the/a scope (it cannot be referenced), but the Variable Object of the global execution context, the global object. However, `window' fails in doing that, it refers to a Window instance (that may have the ECMAScript Global Object in its prototype chain). As the first page of the JavaScript 1.5 Reference says: <https://developer.mozilla.org/en/Core_Javascript_1.5_Reference> | The global object itself can be accessed by `this' in the global scope. (whereas `scope' should read `execution context' there; objections?) Assign it to a variable (I used `_global') or property (I am switching to jsx.global) when needed in a local execution context. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk> |