1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Example where we create an object with a couple of sample properties. 16// (Note that the second parameter maps keys to *property descriptors*.) 17var o = Object.create(Object.prototype, { 18 // foo is a regular 'value property' 19 foo: { writable: true, configurable: true, value: 'hello' }, 20 // bar is a getter-and-setter (accessor) property 21 bar: { 22 configurable: false, 23 get: function() { return 10; }, 24 set: function(value) { console.log('Setting `o.bar` to', value); } 25 } 26}); 27 28// create a new object whose prototype is a new, empty object 29// and a adding single property 'p', with value 42 30var o = Object.create({}, { p: { value: 42 } }); 31// by default properties ARE NOT writable, enumerable or configurable: 32o.p = 24; 33assert (o.p === 42); 34 35// to specify an ES3 property 36var o2 = Object.create({}, { 37 p: { 38 value: 42, 39 writable: true, 40 enumerable: true, 41 configurable: true 42 } 43}); 44 45assert (o2.p === 42); 46 47// Shape - superclass 48function Shape() { 49 this.x = 0; 50 this.y = 0; 51} 52 53// superclass method 54Shape.prototype.move = function(x, y) { 55 this.x += x; 56 this.y += y; 57}; 58 59// Rectangle - subclass 60function Rectangle() { 61 Shape.call(this); // call super constructor. 62} 63 64// subclass extends superclass 65Rectangle.prototype = Object.create(Shape.prototype); 66Rectangle.prototype.constructor = Rectangle; 67 68var rect = new Rectangle(); 69 70assert (rect instanceof Rectangle); 71assert (rect instanceof Shape); 72rect.move(1, 1); 73assert (rect.x === 1) 74assert (rect.y === 1); 75 76var obj = { 77 protoFunction: function() { 78 return 3; 79 } 80}; 81 82Object.defineProperties(obj, { 83 "foo": { 84 value: 42, 85 writable: true, 86 }, 87 "a": { 88 value: "b", 89 configurable: true 90 }, 91 "bar": { 92 get: function() { 93 return this.foo; 94 }, 95 }, 96}); 97 98var obj2 = Object.create(obj); 99 100assert (obj2.protoFunction() === 3); 101assert (obj2.foo === 42); 102assert (obj2.a === "b"); 103assert (obj2.bar === 42); 104assert (Object.getPrototypeOf (obj2) === obj); 105 106 107var props = { 108 prop1: { 109 value: 1, 110 }, 111 hey: function () { 112 return "ho"; 113 } 114}; 115 116var obj3 = Object.create(obj, props); 117assert (obj3.prop1 === 1); 118assert (obj3.protoFunction() === 3); 119try { 120 assert (obj3.hey === undefined); 121 obj3.hey(); 122 assert (false); 123} catch (e) { 124 assert (e instanceof TypeError); 125} 126 127// Create an object with null as prototype 128var obj = Object.create(null) 129assert (typeof (obj) === "object"); 130assert (Object.getPrototypeOf (obj) === null); 131 132try { 133 Object.create() 134 assert (false); 135} catch (e) { 136 assert (e instanceof TypeError); 137} 138 139try { 140 Object.create(undefined) 141 assert (false); 142} catch (e) { 143 assert (e instanceof TypeError); 144} 145