author | martin |
Wed, 10 Dec 2014 09:23:00 -0800 | |
changeset 28057 | 1a47ceecdba5 |
parent 24778 | 2ff5d7041566 |
permissions | -rw-r--r-- |
18860
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
1 |
/* |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
18860
diff
changeset
|
4 |
* |
18860
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
18860
diff
changeset
|
8 |
* |
18860
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
13 |
* accompanied this code). |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
18860
diff
changeset
|
14 |
* |
18860
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
18860
diff
changeset
|
18 |
* |
18860
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
21 |
* questions. |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
22 |
*/ |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
23 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
24 |
/** |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
25 |
* JDK-8014785: Ability to extend global instance by binding properties of another object |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
26 |
* |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
27 |
* @test |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
28 |
* @run |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
29 |
*/ |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
30 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
31 |
var obj = { x: 34, y: 100 }; |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
32 |
var foo = {} |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
33 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
34 |
// bind properties of "obj" to "foo" obj |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
35 |
Object.bindProperties(foo, obj); |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
36 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
37 |
// now we can access/write on foo properties |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
38 |
print("foo.x = " + foo.x); // prints obj.x which is 34 |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
39 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
40 |
// update obj.x via foo.x |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
41 |
foo.x = "hello"; |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
42 |
print("obj.x = " + obj.x); // prints "hello" now |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
18860
diff
changeset
|
43 |
|
18860
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
44 |
obj.x = 42; // foo.x also becomes 42 |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
45 |
print("obj.x = " + obj.x); // prints 42 |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
46 |
print("foo.x = " + foo.x); // prints 42 |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
47 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
48 |
// now bind a mirror object to an object |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
49 |
var obj = loadWithNewGlobal({ |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
50 |
name: "test", |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
51 |
script: "obj = { x: 33, y: 'hello' }" |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
52 |
}); |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
53 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
54 |
Object.bindProperties(this, obj); |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
55 |
print("x = " + x); // prints 33 |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
56 |
print("y = " + y); // prints "hello" |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
57 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
58 |
x = Math.PI; // changes obj.x to Math.PI |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
59 |
print("obj.x = " +obj.x); // prints Math.PI |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
60 |
|
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
61 |
obj.y = 32; |
e387fde9322a
8014785: Ability to extend global instance by binding properties of another object
sundar
parents:
diff
changeset
|
62 |
print("y = " + y); // should print 32 |