getset-002.js (961B)
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 var t = { 8 _y: "<initial y>", 9 10 get y() 11 { 12 var rv; 13 if (typeof this._y == "string") 14 rv = "got " + this._y; 15 else 16 rv = this._y; 17 18 return rv; 19 }, 20 21 set y(newVal) 22 { 23 this._y = newVal; 24 } 25 } 26 27 28 test(t); 29 30 function test(t) 31 { 32 printStatus ("Basic Getter/ Setter test (object literal notation)"); 33 34 reportCompare ("<initial y>", t._y, "y prototype check"); 35 36 reportCompare ("got <initial y>", t.y, "y getter, before set"); 37 38 t.y = "new y"; 39 reportCompare ("got new y", t.y, "y getter, after set"); 40 41 t.y = 2; 42 reportCompare (2, t.y, "y getter, after numeric set"); 43 44 var d = new Date(); 45 t.y = d; 46 reportCompare (d, t.y, "y getter, after date set"); 47 48 }