uncategorized

my testing JS in Node.js

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; // variable of the global context

(function () {
var b = 20; // local variable of the function context
})();

console.log(a); // 10
// check: console.log(b); // "b" is not defined
/*
* 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 "k" still in scope even the loop is finished

/*
* Variable object in global context
*/


var a = new String('test');
b = 10;

console.log(a); // ��������, ����� ������� � VO(globalContext): "test"

//node.js fix console.log(window['a']); // �������� ����� global === VO(globalContext): "test"
console.log(a === this.a); // true(browser) : false in node.js this.a is undefined

exports.a = a;
console.log(a === this.a); // true beacause this is exports;

// var aKey = 'a';
// console.log(window[aKey]); // ��������, ��� �������� ������������ ������: "test"

/*
* 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) {

// quantity of defined function arguments (x, y, z)
console.log('foo.length: '+ foo.length); // 3

// quantity of really passed arguments (only x, y)
console.log('arguments.length:' + arguments.length); // 2

// reference of a function to itself
console.log('arguments.callee === foo: ' + (arguments.callee === foo)); // true

// parameters sharing

console.log('x === arguments[0]: ' + (x === arguments[0])); // true
console.log(x); // 10

arguments[0] = 20;
console.log(x); // 20

x = 30;
console.log('arguments[0]: '+arguments[0]); // 30

// however, for not passed argument z,
// related index-property of the arguments
// object is not shared

z = 40;
console.log('arguments[2]:'+ arguments[2]); // undefined

arguments[2] = 50;
console.log(z); // 40

}

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;