![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following: eval ('localVar = new ' + objType.value + '(val1, val2, val3'); localVar should refer to a variable local to the calling object, but with the eval approach it ends up being the local variable for the scope from which the eval was first executed. |
|
I could use a seitch statement, but this approach means I now have an extra place where I need to change my code so I'd like to afford this if possible. Is there another approach to creating a new instance of an object dependant on the value of some variable? |
#3
| |||
| |||
|
|
Gordon schreef: I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following: eval ('localVar = new ' + objType.value + '(val1, val2, val3'); localVar should refer to a variable local to the calling object, but with the eval approach it ends up being the local variable for the scope from which the eval was first executed. Hi Gordon, So the scope is the problem, not eval? Right? I guess your eval-code is in some function. Without knowing anything about your code, and who calls what, what about returning the object? So: eval ('var localVar = new ' + objType.value + '(val1, val2, val3'); return localVar; And when calling the routine: theObj = myEvalRoutine(vars here); Could that help? Regards, Erwin Moller I could use a seitch statement, but this approach means I now have an extra place where I need to change my code so I'd like to afford this if possible. Is there another approach to creating a new instance of an object dependant on the value of some variable? |
#4
| |||
| |||
|
|
The eval was a quick and dirty alternative to a switch statement for each kind of object that i want to be able to instantize. The problem is that using the eval is screwing up the scoping rules that normally apply in javascript that involve coming up with inelegant workarounds, |
|
such as putting stuff that I'd normally keep in the scope of the cose I'm running in the global scope instead. |
|
Of the two choices the switch statement is probably the better one, but it adds an extra point in the code that has to be changed should, for example, I choose to add a new object type. It also adds n lines of code per object type, and I'd like to find a better solution. I guess what I really want to be able to do is something like: var localVar = new objType.value () But when I try that I just get an objType.value is not a function message. |
#5
| ||||||
| ||||||
|
|
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); |
.|
objType is a select with the different types of objects you can create as values. |
|
localVar should refer to a variable local to the calling object, |
|
but with the eval approach it ends up being the local variable for the scope from which the eval was first executed. |
|
I could use a seitch statement, but this approach means I now have an extra place where I need to change my code so I'd like to afford this if possible. |
|
Is there another approach to creating a new instance of an object dependant on the value of some variable? |

#6
| |||
| |||
|
|
Gordon <gordon.mc... (AT) ntlworld (DOT) com> writes: I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); Reduce the scope of eval as much as possible (preferably to nothing .This can be reduced to: new (eval(objType.value))(val1, val2, val3); (or, more readable: var Constructor = eval(objType.value); ... new Constructor(val1, val2, val3) ... ) objType is a select with the different types of objects you can create as values. Who created the objType objects? Why do they contain the names of functions instead of the functions themselves? There can be good reasons for it, but if it is possible to rewrite the data so that the objType contains the actual constructor function instead of, or in addition to, its name, then many things become simpler. localVar should refer to a variable local to the calling object, What does that mean? Objects do not have variables. Scopes have variables, and objects have properties. but with the eval approach it ends up being the local variable for the scope from which the eval was first executed. That's what the code would mean without the eval too: localVar = new (eval(objType.value))(val1,val2,val3); assigns to the "localVar" variable in the current scope chain. I could use a seitch statement, but this approach means I now have an extra place where I need to change my code so I'd like to afford this if possible. Your problem is that you need to lookup a function by its name. The current solution is to look it up in the scope, and store the function in the global scope. That causes the same token to do double duty: as a function name and property of the global object, and as a data value in your data. I prefer to keep those two namespaces (language/data) separate if possible. You could create a lookup-table: var functionTable = { objType1: Type1Constructor, objType2: Type2Constructor, ... objTypeN: typeNConstructor }; Then instead of eval, you could do an explicit lookup: localVar = new (functionTable[objType.value])(val1, val2, val3); Is there another approach to creating a new instance of an object dependant on the value of some variable? To create new objects from a constructor, first find the constructor function. That is the part you are using eval for. I recommend against it, but it might be so much easier than alternatives that you choose to stick with it. Just keep the scope of eval down, in this case to a single variable (which can then be replaced by "window[objType.value]" ![]() Then use the "new" operator. If you know the number of arguments (or a reasonable upper bound on them), then there is no need for eval in that. Remember that, unlike, e.g., Java, you can use any variable to refer to the constructor function in the "new" expression, so function MyType(){} var t1 = new MyType(); var x = MyType; var t2 = new x(); is perfectly valid and there is no difference in properties and prototype chain between the values of t1 and t2. That is why you can separate the lookup of the constructor from the creation of a new instance. Constructors are not types, they are just function value like every other function. /L -- Lasse Reichstein Nielsen - l... (AT) hotpop (DOT) com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html 'Faith without judgement merely degrades the spirit divine.' |
I still would rather eliminate
#7
| |||
| |||
|
|
On Jun 12, 5:38 pm, Henry <rcornf... (AT) raindrop (DOT) co.uk> wrote: On Jun 12, 5:03 pm, Gordon wrote: [...] var localVar = new objType.value () But when I try that I just get an objType.value is not a function message. As is should, but if you provided some context for you code you might get a decent answer to your question. Which will be how a bracket notation property access can be employed here. For example, if all your constructors were globally declared functions and environment was a web browser were no alternative means of accessing the global object had been created those constructors could be references as members of the object referred to by - window -. I.E.:- var localVar = new window[objType.value](); Here's the complete code. Bear in mind it's in a very early stage of development, is very rough and ready, and has lots of code in the middle of being rewritten [ca. 400 lines of uncommented clueless source code] |
#8
| |||
| |||
|
|
Richard's implicitly asking for "some context" was certainly not to be misunderstood as a request for your dumping core here and letting others do your homework, in their leisure-time, for free. |
![]() |
| Thread Tools | |
| Display Modes | |
| |