Prev: Native Objects .prototype extending
Next: The 3rd way
From: Thomas 'PointedEars' Lahn on 28 Nov 2009 10:28 Asen Bozhilov wrote: > Thomas 'PointedEars' Lahn 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 your variant of the `with' approach does not solve the problem of using an identifier defined at runtime without using eval() either (using a writing bracket property accessor to add the property of the object inserted in the scope chain would), nor would it be necessary (the reading bracket property accessor would suffice). 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 15:34 On Nov 28, 5:51 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: [...] > > Dmitry that isn't only approach to "declare" local variable. > What exactly do you mean? The only way to declare variable is to use `var` keyword. If to summarize points which could be related to `variable` (regardless syntax rules such as `VariableStatement` - 12.2, ES-3) we can get the following: (1) affecting on the VO/AO; (2) created on entering execution context (main sign); (3) get {DontDelete} attribute unless type of code is `eval`. The point (1) is used to show that not every named object is affected on VO/AO, e.g. named function expression (NFE) which by specification should keep optional identifier in special object added in front of scope chain but not in VO/AO itself; but as you know there're some non- conformant impementations as some versions of JScript, including current version. The point (2) I suggest to use as a main sign to distinguish them from the properties added to the special object of `with` statement and `catch` clause. The two last will be available in the scope chain at runtime, but not right after entering the execution context. Moreover, regarding `with`, it doesn't declare that properties of its special object should have {DontDelete} attributes, it depends on object itself. So, in simple case we can delete this property: with ({a: 10}) { alert(typeof a); // number delete a; alert(typeof a); // undefined } But we can't if properties already have {DontDelete} var a = 10; with (this) { alert(typeof a); // number delete z; // error in JScript alert(typeof a); // number, as `a` has {DontDelete} } From the other hand, not every variable declaration should get {DontDelete} as it depends on type of code - in `eval` code they shouldn't get {DontDelete}, so this point (3) is not the main. But about point (2) regarding to execution context of function code type, formal parameters also should get {DontDelete} depending on code's type. So from this point of view formal parameters are the same as variables - they created on entering execution context and should have {DontDelete} depending on type of the code. But in difference from variables, formal parameters get values (if corresponding values are passed) also on entering the execution context, meanwhile variable always get `undefined` on this step. Regarding {DontDelete} and function's formal parameters, there's non- conformant issue in Chrome - it doesn't set {DontDelete} in this case. function test(a, b) { alert([typeof a, typeof b]); // number, number delete a; delete arguments[1]; // undefined, undefined - in Chrome // number, number - in other alert([typeof a, typeof b]); } Also, regarding property of special object added by `catch` clause, current version of Opera doesn't set {DontDelete} to it. try { trow 'error'; } catch (e) { alert(typeof e); // string delete e; // undefined - in current Opera // string - in other alert(typeof e); } So, summarizing this all, variable is: added to the VO/AO on entering the execution context (that differs it from other properties) and _always_ has `undefined` value on this step (that differs it from the formal parameter, which can have value any, but not only `undefined` on this step). What other approaches to "declare" local variable did you mean? /ds
From: Asen Bozhilov on 28 Nov 2009 19:13 Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > But your variant of the `with' approach does not solve the problem of using > an identifier defined at runtime without using eval() either (using a > writing bracket property accessor to add the property of the object inserted > in the scope chain would), nor would it be necessary (the reading bracket > property accessor would suffice). That isn't true. That property will be resolve not only from Prototype Chain. That property will be resolve from Prototype Chain and Scope Chain, that can be benefit in some cases. Of course that is point of view and what exactly want to do it. If i write sometime code like this. Maybe i'll be use just only one object without modified Scope Chain with `with' statement.
From: Asen Bozhilov on 28 Nov 2009 20:10 Dmitry A. Soshnikov wrote: > What exactly do you mean? The only way to declare variable is to use > `var` keyword. I m not going to assert another thesis. > If to summarize points which could be related to > `variable` (regardless syntax rules such as `VariableStatement` - > 12.2, ES-3) we can get the following: > > (1) affecting on the VO/AO; > (2) created on entering execution context (main sign); > (3) get {DontDelete} attribute unless type of code is `eval`. > > The point (1) is used to show that not every named object is affected > on VO/AO, e.g. named function expression (NFE) which by specification > should keep optional identifier in special object added in front of > scope chain but not in VO/AO itself; but as you know there're some non- > conformant impementations as some versions of JScript, including > current version. Of course but, using indirect call of `eval' at the moment you can be sure to defined local variable, because `eval' use VO of calling execution context. ECMA 262-3 | 10.2.2 Eval Code | Variable instantiation is performed using the calling context's | variable object and using empty property attributes. But in ECMA 5 in strict mode you cannot be create local variable using indirect call of `eval' | 10.4.2.1 Strict Mode Restrictions | The eval code cannot instantiate variable or | function bindings in the variable environment of the calling | context that invoked the eval if either the code | of the calling context or the eval code is strict code. Instead | such bindings are instantiated in a new | VariableEnvironment that is only accessible to the eval code. > The point (2) I suggest to use as a main sign to distinguish them from > the properties added to the special object of `with` statement and > `catch` clause. The two last will be available in the scope chain at > runtime, but not right after entering the execution context. `eval' defined properties of VO runtime too. > Moreover, regarding `with`, it doesn't declare that properties of its > special object should have {DontDelete} attributes, it depends on > object itself. So, in simple case we can delete this property: > > with ({a: 10}) { > alert(typeof a); // number > delete a; > alert(typeof a); // undefined > > } No, here depend from internal attribute {DontDelete} of property which want to deleted. > So, summarizing this all, variable is: added to the VO/AO on entering > the execution context (that differs it from other properties) and > _always_ has `undefined` value on this step (that differs it from the > formal parameter, which can have value any, but not only `undefined` > on this step). > Yes. I know all of that. I want to give just another example to create property which: - Resolve from Scope Chain. - Doesn't has any reference to that property in normal case after `with' statement. > What other approaches to "declare" local variable did you mean? Should be know? :) Regards.
From: Thomas 'PointedEars' Lahn on 28 Nov 2009 21:11 Asen Bozhilov wrote: > Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: >> But your variant of the `with' approach does not solve the problem of >> using an identifier defined at runtime without using eval() either (using >> a writing bracket property accessor to add the property of the object >> inserted in the scope chain would), nor would it be necessary (the >> reading bracket property accessor would suffice). > > That isn't true. That property will be resolve not only from Prototype > Chain. That property will be resolve from Prototype Chain and Scope > Chain, that can be benefit in some cases. Of course that is point of > view and what exactly want to do it. If i write sometime code like > this. Maybe i'll be use just only one object without modified Scope > Chain with `with' statement. Either you misunderstood what the OP was asking or you misunderstood what I was replying. The OP was (also) looking for an alternative to function foo(runtimeInstanceName) { eval("var " + runtimeInstanceName + " = new Foo();"); } foo("bar"); to have the equivalent of function foo() { var bar = new Foo(); } Your suggestion, with ({'instance_name' : new Foo()}) does not help to solve this problem because if you wrote function foo(runtimeInstanceName) { with ({runtimeInstanceName: new Foo()}) { bar; } } foo("bar"); that would be the equivalent of function foo() { with ({runtimeInstanceName: new Foo()}) { /* * ReferenceError, unless Object.prototype * provides a 'bar' property */ bar; } } You could write function foo(runtimeInstanceName) { var o = {}; o[runtimeInstanceName] = 'bar'; with (o) { // ... } } But then you would not need the `with' statement in the first place as `o[runtimeInstanceName]' is readily available for read access and it is no longer a matter of declaring anything. PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Native Objects .prototype extending Next: The 3rd way |