1425bb815Sopenharmony_ci// Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci//
3425bb815Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci// you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci// You may obtain a copy of the License at
6425bb815Sopenharmony_ci//
7425bb815Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci//
9425bb815Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci// See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci// limitations under the License.
14425bb815Sopenharmony_ci
15425bb815Sopenharmony_ci// Example where we create an object with a couple of sample properties.
16425bb815Sopenharmony_ci// (Note that the second parameter maps keys to *property descriptors*.)
17425bb815Sopenharmony_civar o = Object.create(Object.prototype, {
18425bb815Sopenharmony_ci  // foo is a regular 'value property'
19425bb815Sopenharmony_ci  foo: { writable: true, configurable: true, value: 'hello' },
20425bb815Sopenharmony_ci  // bar is a getter-and-setter (accessor) property
21425bb815Sopenharmony_ci  bar: {
22425bb815Sopenharmony_ci    configurable: false,
23425bb815Sopenharmony_ci    get: function() { return 10; },
24425bb815Sopenharmony_ci    set: function(value) { console.log('Setting `o.bar` to', value); }
25425bb815Sopenharmony_ci  }
26425bb815Sopenharmony_ci});
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci// create a new object whose prototype is a new, empty object
29425bb815Sopenharmony_ci// and a adding single property 'p', with value 42
30425bb815Sopenharmony_civar o = Object.create({}, { p: { value: 42 } });
31425bb815Sopenharmony_ci// by default properties ARE NOT writable, enumerable or configurable:
32425bb815Sopenharmony_cio.p = 24;
33425bb815Sopenharmony_ciassert (o.p === 42);
34425bb815Sopenharmony_ci
35425bb815Sopenharmony_ci// to specify an ES3 property
36425bb815Sopenharmony_civar o2 = Object.create({}, {
37425bb815Sopenharmony_ci  p: {
38425bb815Sopenharmony_ci    value: 42,
39425bb815Sopenharmony_ci    writable: true,
40425bb815Sopenharmony_ci    enumerable: true,
41425bb815Sopenharmony_ci    configurable: true
42425bb815Sopenharmony_ci  }
43425bb815Sopenharmony_ci});
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_ciassert (o2.p === 42);
46425bb815Sopenharmony_ci
47425bb815Sopenharmony_ci// Shape - superclass
48425bb815Sopenharmony_cifunction Shape() {
49425bb815Sopenharmony_ci  this.x = 0;
50425bb815Sopenharmony_ci  this.y = 0;
51425bb815Sopenharmony_ci}
52425bb815Sopenharmony_ci
53425bb815Sopenharmony_ci// superclass method
54425bb815Sopenharmony_ciShape.prototype.move = function(x, y) {
55425bb815Sopenharmony_ci  this.x += x;
56425bb815Sopenharmony_ci  this.y += y;
57425bb815Sopenharmony_ci};
58425bb815Sopenharmony_ci
59425bb815Sopenharmony_ci// Rectangle - subclass
60425bb815Sopenharmony_cifunction Rectangle() {
61425bb815Sopenharmony_ci  Shape.call(this); // call super constructor.
62425bb815Sopenharmony_ci}
63425bb815Sopenharmony_ci
64425bb815Sopenharmony_ci// subclass extends superclass
65425bb815Sopenharmony_ciRectangle.prototype = Object.create(Shape.prototype);
66425bb815Sopenharmony_ciRectangle.prototype.constructor = Rectangle;
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_civar rect = new Rectangle();
69425bb815Sopenharmony_ci
70425bb815Sopenharmony_ciassert (rect instanceof Rectangle);
71425bb815Sopenharmony_ciassert (rect instanceof Shape);
72425bb815Sopenharmony_cirect.move(1, 1);
73425bb815Sopenharmony_ciassert (rect.x === 1)
74425bb815Sopenharmony_ciassert (rect.y === 1);
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_civar obj = {
77425bb815Sopenharmony_ci  protoFunction: function() {
78425bb815Sopenharmony_ci    return 3;
79425bb815Sopenharmony_ci  }
80425bb815Sopenharmony_ci};
81425bb815Sopenharmony_ci
82425bb815Sopenharmony_ciObject.defineProperties(obj, {
83425bb815Sopenharmony_ci  "foo": {
84425bb815Sopenharmony_ci    value: 42,
85425bb815Sopenharmony_ci    writable: true,
86425bb815Sopenharmony_ci  },
87425bb815Sopenharmony_ci  "a": {
88425bb815Sopenharmony_ci    value: "b",
89425bb815Sopenharmony_ci    configurable: true
90425bb815Sopenharmony_ci  },
91425bb815Sopenharmony_ci  "bar": {
92425bb815Sopenharmony_ci    get: function() {
93425bb815Sopenharmony_ci      return this.foo;
94425bb815Sopenharmony_ci    },
95425bb815Sopenharmony_ci  },
96425bb815Sopenharmony_ci});
97425bb815Sopenharmony_ci
98425bb815Sopenharmony_civar obj2 = Object.create(obj);
99425bb815Sopenharmony_ci
100425bb815Sopenharmony_ciassert (obj2.protoFunction() === 3);
101425bb815Sopenharmony_ciassert (obj2.foo === 42);
102425bb815Sopenharmony_ciassert (obj2.a === "b");
103425bb815Sopenharmony_ciassert (obj2.bar === 42);
104425bb815Sopenharmony_ciassert (Object.getPrototypeOf (obj2) === obj);
105425bb815Sopenharmony_ci
106425bb815Sopenharmony_ci
107425bb815Sopenharmony_civar props = {
108425bb815Sopenharmony_ci    prop1: {
109425bb815Sopenharmony_ci        value: 1,
110425bb815Sopenharmony_ci    },
111425bb815Sopenharmony_ci    hey: function () {
112425bb815Sopenharmony_ci        return "ho";
113425bb815Sopenharmony_ci    }
114425bb815Sopenharmony_ci};
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_civar obj3 = Object.create(obj, props);
117425bb815Sopenharmony_ciassert (obj3.prop1 === 1);
118425bb815Sopenharmony_ciassert (obj3.protoFunction() === 3);
119425bb815Sopenharmony_citry {
120425bb815Sopenharmony_ci  assert (obj3.hey === undefined);
121425bb815Sopenharmony_ci  obj3.hey();
122425bb815Sopenharmony_ci  assert (false);
123425bb815Sopenharmony_ci} catch (e) {
124425bb815Sopenharmony_ci  assert (e instanceof TypeError);
125425bb815Sopenharmony_ci}
126425bb815Sopenharmony_ci
127425bb815Sopenharmony_ci// Create an object with null as prototype
128425bb815Sopenharmony_civar obj = Object.create(null)
129425bb815Sopenharmony_ciassert (typeof (obj) === "object");
130425bb815Sopenharmony_ciassert (Object.getPrototypeOf (obj) === null);
131425bb815Sopenharmony_ci
132425bb815Sopenharmony_citry {
133425bb815Sopenharmony_ci    Object.create()
134425bb815Sopenharmony_ci    assert (false);
135425bb815Sopenharmony_ci} catch (e) {
136425bb815Sopenharmony_ci    assert (e instanceof TypeError);
137425bb815Sopenharmony_ci}
138425bb815Sopenharmony_ci
139425bb815Sopenharmony_citry {
140425bb815Sopenharmony_ci    Object.create(undefined)
141425bb815Sopenharmony_ci    assert (false);
142425bb815Sopenharmony_ci} catch (e) {
143425bb815Sopenharmony_ci    assert (e instanceof TypeError);
144425bb815Sopenharmony_ci}
145