function Foo(bar){ this._bar = bar; } Foo.prototype = { get bar () { return this._bar; }, set bar (bar) { this._bar = bar; } };
function Foo(bar) { this._bar = bar; }; Foo.prototype = { setBar : function (bar) { this._bar = bar; }, getBar : function () { return this._bar; } }; var foo = new Foo; foo.setBar(123); alert(foo.getBar());
function Foo(bar) { var _bar = bar; this.bar = function (bar) { if (arguments.length) _bar = bar; else return _bar; } }; var foo = new Foo; foo.bar(123); alert(foo.bar());
for (var i in topics.comments); // vs for (var i in topics.loadComments());
function Foo(bar) { this._bar = bar; }; Foo.prototype = { set bar (bar) { this._bar = bar; }, get bar () { return this._bar; } }; var foo = new Foo; foo.bar = 123; alert(foo.bar);
function Foo(bar) { var _bar = bar; this.__defineGetter__("bar", function(){ return _bar; }); this.__defineSetter__("bar", function(val){ _bar = bar; }); }; var foo = new Foo; foo.bar = 123; alert(foo.bar);
return (typeof {}.__defineGetter__ == 'function');
.__lookupGetter__
and .__lookupSetter__
. function extend(target, source) { for ( var prop in source ) { var getter = source.__lookupGetter__(prop), setter = source.__lookupSetter__(prop); if ( getter || setter ) { if ( getter ) target.__defineGetter__(prop, getter); if ( setter ) target.__defineSetter__(prop, setter); } else a[i] = b[i]; } return target; }
* For each property, you can set only one getter and / or setter. There can be two getters or setters
* The only way to remove a getter or setter is to calldelete object[name];
. This command deletes both the getter and the setter, so if you want to delete one thing and leave the other, you must first save it, and after deleting it, assign it again
* When you use __defineGetter__ or __defineSetter__, it just silently overwrites the previous getter or setter, and even deletes the property itself with that name.
* You can check if your browser supports getters and setters using a simple snippet:
javascript:foo={get test(){ return "foo"; }};alert(foo.test);
var Foo = new Class({ set test : function () { console.log('test is set'); }, get test : function () { console.log('test is got'); return 'test'; }, }); foo.test = 1234; // test is set alert(foo.test); // test is get
var implementGettersSetters = (typeof {}.__lookupGetter__ == 'function') ? function (target, source) { for (var key in source) { var g = source.__lookupGetter__(key), s = source.__lookupSetter__(key); if ( g || s ) { if (g) target.__defineGetter__(key, g); if (s) target.__defineSetter__(key, s); } } return target; } : function (target, source) { return target; };
// var Class = this.Class = new Type('Class', function(params){ // newClass.$constructor = Class; newClass.prototype.$constructor = newClass; newClass.prototype.parent = parent; // , (, , !) : implementGettersSetters(newClass.prototype, params);
Class.Mutators = { // ... Implements: function(items){ Array.from(items).each(function(item){ var instance = new item; for (var key in instance) implement.call(this, key, instance[key], true); // : implementGettersSetters(this.prototype, instance); }, this); } };
var Foo = new Class({ initialize : function (name) { this.name = name; }, set test : function () { console.log(this.name + ' test is set'); }, get test : function () { console.log(this.name + ' test is got'); return 'test'; }, }); var Bar = new Class({ Extends : Foo }); var Qux = new Class({ Implements : [ Foo ] }); var foo = new Foo('foo'); foo.test = 1234; // foo test is set alert(foo.test); // foo test is got var bar = new Bar('bar'); bar.test = 1234; // bar test is set alert(bar.test); // bar test is got var qux = new Qux('qux'); qux.test = 1234; // qux test is set alert(qux.test); // qux test is got
Object.defineProperty
- a tool for creating properties with very wide settings, such as writable, get, set, configurable, enumerableObject.create
- it is convenient to quickly create the necessary objects.Source: https://habr.com/ru/post/108295/
All Articles