getset-001.js (1155B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 7 function TestObject () 8 { 9 /* A warm, dry place for properties and methods to live */ 10 } 11 12 TestObject.prototype._y = "<initial y>"; 13 14 Object.defineProperty(TestObject.prototype, "y", 15 { 16 enumerable: true, configurable: true, 17 get: function get_y () 18 { 19 var rv; 20 if (typeof this._y == "string") 21 rv = "got " + this._y; 22 else 23 rv = this._y; 24 return rv; 25 }, 26 set: function set_y (newVal) { this._y = newVal; } 27 }); 28 29 30 test(new TestObject()); 31 32 function test(t) 33 { 34 printStatus ("Basic Getter/ Setter test"); 35 reportCompare ("<initial y>", t._y, "y prototype check"); 36 37 reportCompare ("got <initial y>", t.y, "y getter, before set"); 38 39 t.y = "new y"; 40 reportCompare ("got new y", t.y, "y getter, after set"); 41 42 t.y = 2; 43 reportCompare (2, t.y, "y getter, after numeric set"); 44 45 var d = new Date(); 46 t.y = d; 47 reportCompare (d, t.y, "y getter, after date set"); 48 49 }