objectprops.js revision 877:cf4d2252d444
142421Syokota/* 242421Syokota * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 342421Syokota * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 442421Syokota * 542421Syokota * This code is free software; you can redistribute it and/or modify it 642421Syokota * under the terms of the GNU General Public License version 2 only, as 742421Syokota * published by the Free Software Foundation. 842421Syokota * 942421Syokota * This code is distributed in the hope that it will be useful, but WITHOUT 1042421Syokota * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1142421Syokota * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1242421Syokota * version 2 for more details (a copy is included in the LICENSE file that 1342421Syokota * accompanied this code). 1442421Syokota * 1542421Syokota * You should have received a copy of the GNU General Public License version 1642421Syokota * 2 along with this work; if not, write to the Free Software Foundation, 1742421Syokota * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1842421Syokota * 1942421Syokota * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2042421Syokota * or visit www.oracle.com if you need additional information or have any 2142421Syokota * questions. 2242421Syokota */ 2342421Syokota 2442421Syokota/** 2542421Syokota * Check Object.defineProperty, Object.defineProperties and Object.create. 2642421Syokota * FIXME: yet to checks for property attribute create/modifications. 2742421Syokota * 28119418Sobrien * @test 29119418Sobrien * @run 30119418Sobrien */ 3142421Syokota 3242421Syokota// create an object 3342421Syokotavar obj = {}; 3442421Syokota// data property 3542421SyokotaObject.defineProperty(obj, "foo", { value: "hello" }); 3642421Syokota// accessor property 3742421SyokotaObject.defineProperty(obj, "bar", { get: function() { return "bar" } }); 38139193Sphk 3942421Syokotaprint("obj.foo = " + obj.foo); 4042421Syokotaprint("obj.bar = " + obj.bar); 41112050Sdwmalone 42112050Sdwmalone// define multiple properties at one go. 4342421SyokotaObject.defineProperties(obj, 4442421Syokota { 4566834Sphk xyz: { value: 44 }, 4642421Syokota abc: { get: function() { print("get abc"); return "abc"; } } 4742421Syokota } 4842421Syokota); 4950154Syokota 5050154Syokotaprint("obj.xyz = " + obj.xyz); 5150154Syokotaprint("obj.abc = " + obj.abc); 5250154Syokota 5350154Syokotafunction MyConstructor() {} 5450154Syokotavar obj2 = Object.create(MyConstructor.prototype); 5550154Syokotaprint("obj2 in MyConstructor instance? " + (obj2 instanceof MyConstructor)); 5650154Syokota 5750154Syokotavar obj3 = Object.create(Object.prototype, 5860938Sjake { 59127751Sdes xyz: { value: 44 } 6054545Syokota } 6178161Speter); 6278161Speter 6342421Syokotaprint("obj3 is an Object? " + (obj3 instanceof Object)); 6442421Syokotaprint(obj3.xyz); 6542421Syokota