1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| console.log('Standard ECMA-262 3rd Edition - December 1999:'); console.log('Variable object:');
* Always in programs we declare functions and variables which then successfully use building our systems. * But how and where the interpreter finds our data (functions, variable)? * What occurs, when we reference to needed objects? * Many ECMAScript programmers know that variables are closely related with the execution context: * */
var a = 10;
(function () { var b = 20; })();
console.log(a);
* Also, many programmers know that the isolated scope in the current version of specification is created * only by execution contexts with �function� code type. I.e., in contrast with C/C++, for example * the block of for loop in ECMAScript does not create a local context: * */
for (var k in { a: 1, b: 2 }) { console.log('k'+ k); }
console.log('k' + k);
* Variable object in global context */
var a = new String('test'); b = 10;
console.log(a);
console.log(a === this.a);
exports.a = a; console.log(a === this.a);
* Variable object in function context * * Regarding the execution context of functions � there VO is inaccessible directly, * and its role plays so - called an activation object(in abbreviated form � AO). * * VO(functionContext) === AO; * * An activation object is created on entering the context of a function and initialized * by property arguments which value is the Arguments object: * * AO ={ * arguments: <ArgO> * }; * * * Arguments object is a property of the activation object. It contains the following properties: * callee � the reference to the current function; * length � quantity of real passed arguments; * properties-indexes (integer, converted to string) which values are the values of function�s arguments * (from left to right in the list of arguments). * Quantity of these properties-indexes == arguments.length. * Values of properties-indexes of the arguments object and present (really passed) formal parameters are shared. */
function foo(x, y, z) { console.log('foo.length: '+ foo.length); console.log('arguments.length:' + arguments.length); console.log('arguments.callee === foo: ' + (arguments.callee === foo)); console.log('x === arguments[0]: ' + (x === arguments[0])); console.log(x); arguments[0] = 20; console.log(x); x = 30; console.log('arguments[0]: '+arguments[0]); z = 40; console.log('arguments[2]:'+ arguments[2]); arguments[2] = 50; console.log(z); }
foo(10, 20);
* Feature of implementations: property __parent__ */
* As it was already noted, by the standard, to get direct access to the activation object is impossible. * However, in some implementations, namely in SpiderMonkey and Rhino, functions have special property __parent__, * which is the reference to the activation object (or the global variable object) * in which these functions have been created. Let's test. * // console.log(foo.__parent__); So in node.js __parent__ property doesn't work;
|