-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathAccessors.js
151 lines (128 loc) · 3.42 KB
/
Accessors.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
var o = new Object();
WScript.Echo("*** Setting data property ***");
o.x = 23;
WScript.Echo("o.x=" + o.x);
WScript.Echo("*** Setting data property using defineProperty ***");
Object.defineProperty(o, "x", { value : 24 });
WScript.Echo("o.x=" + o.x);
WScript.Echo("*** Setting accessor property using defineProperty ***");
var x = "";
var z = "";
Object.defineProperty(o, "x", {
get : function() { WScript.Echo("Getter called"); return x; },
set : function(val) { WScript.Echo("Setter called"); z = 1000; x = val; }
});
o.x = 25;
WScript.Echo("o.x=" + o.x);
WScript.Echo("x=" + x);
WScript.Echo("z=" + z);
WScript.Echo("*** Setting backing store for accessor ***");
x = 26;
WScript.Echo("o.x=" + o.x);
var ab = new Object();
Object.defineProperty(ab,"foo",{get:function(){WScript.Echo("In getter");}, configurable: true});
Object.defineProperty(ab,"foo",{set:function(arg){WScript.Echo("In setter");}});
ab.foo;
ab.foo = 10;
delete ab.foo;
try
{
var ab = new Object();
Object.defineProperty(ab,"foo",{get:function(){WScript.Echo("In getter");}});
ab.foo;
ab.foo = 10;
}
catch(e)
{
WScript.Echo(e.description);
}
delete ab.foo;
try
{
var ab = new Object();
Object.defineProperty(ab,"foo",{set:function(arg){WScript.Echo("In setter");}});
WScript.Echo(ab.foo);
ab.foo = 10;
}
catch(e)
{
WScript.Echo(e.description);
}
delete ab.foo;
var o = {};
o.a = 1;
o.b = 2;
o.c = 3;
o.d = 4;
o.e = 5;
o.f = 6;
o.g = 7;
o.h = 8;
o.i = 9;
o.j = 10;
o.k = 11;
o.l = 12;
o.m = 13;
o.n = 14;
o.o = 15;
o.p = 16;
o.q = 17;
Object.defineProperty(o, "qqq",
{
set: function () { },
get: function() { WScript.Echo("get"); }
});
WScript.Echo(o.qqq);
delete o.qqq;
// prototype setter/getter
function Point() {
this.x=0;
this.y=0;
}
Point.prototype = {
print:function() { WScript.Echo("x:"+this.x+", y:"+this.y+", z:"+this.z); }
};
Object.defineProperty(Point.prototype,"z",{ set:function(v) { this._z=v; }, get: function() { return this._z; }});
var pt=new Point();
pt.z=12;
pt.print();
Object.defineProperty(this, "abc",
{
set: function () { },
get: function() { WScript.Echo("get global"); }
});
WScript.Echo(abc);
delete this.abc;
(function () {
WScript.Echo("*** Getters, prototypes, and deleting properties ***");
function A() { };
A.prototype = {
get p () { return this._p; },
set p (v) { this._p = v; }
};
var o = new A();
o.p;
delete A.prototype.p;
o.p;
WScript.Echo(o.p);
WScript.Echo(A.prototype.p);
})();
(function () {
WScript.Echo("*** Setters, prototypes, and deleting properties ***");
function A() { };
A.prototype = {
get p () { return this._p; },
set p (v) { this._p = v; }
};
var o = new A();
o._p = undefined; // create the property to stop the setter from changing the type
o.p = 1;
delete A.prototype.p;
o.p = 2;
WScript.Echo(o.p);
WScript.Echo(A.prototype.p);
})();