-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathDefProp.js
111 lines (93 loc) · 2.52 KB
/
DefProp.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
function def_prop_array()
{
var array = [];
Object.defineProperty(array, 0, { get: function() { WScript.Echo("array[0]"); } });
var sum = 0;
for (var i = 0; i < 3; i++)
{
sum += array[0];
}
}
function def_prop_object()
{
var object = {};
Object.defineProperty(object, "sum", { get: function() { WScript.Echo("sum"); } });
var sum = 0;
for (var i = 0; i < 3; i++)
{
sum += object.sum;
}
}
function def_props_array()
{
var array = [];
Object.defineProperties(array,
{
0: { get: function() { WScript.Echo("array[0]"); } },
1: { get: function() { WScript.Echo("array[1]"); } }
});
var sum = 0;
for (var i = 0; i < 3; i++)
{
sum += array[0];
}
}
function def_props_object()
{
var object = {};
Object.defineProperties(object,
{
sum1: { get: function() { WScript.Echo("sum1"); } },
sum2: { get: function() { WScript.Echo("sum2"); } }
});
var sum = 0;
for (var i = 0; i < 3; i++)
{
sum += object.sum1;
}
}
function def_props_number()
{
function diag() {
WScript.Echo("Type: " + (typeof this));
WScript.Echo(" Is Object: " + (this instanceof Object));
WScript.Echo(" Is Number: " + (this instanceof Number));
}
Object.defineProperty(
Number.prototype,
"foo",
{
set: diag
});
Object.defineProperty(
Number.prototype,
"42",
{
set: diag
});
var runTests = function(obj) {
WScript.Echo("** Testing property 'foo'");
obj.foo = {};
WScript.Echo("");
WScript.Echo("** Testing property 42");
obj[42] = {};
WScript.Echo("");
}
var i = 3;
runTests(i);
var d = 3.14;
runTests(d);
}
function main()
{
def_prop_array();
def_prop_object();
def_props_array();
def_props_object();
def_props_number();
}
main();