|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Inherited Privileged MembersThe strawman articles on Object Initialisers look very promising (http://wiki.ecmascript.org/doku.php?id=strawman:object_initialiser_extensions), but one issue I didn't see discussed is a solution to enable the inheritance of privileged members. Currently developers have to redefine accessor properties and methods on every constructor call to maintain a reference to the instance private variables. Ex:
---------------------------------------------------------------- function Foo(name, value){ // assume there was argument validation of some form. var _name = name, _value = value; this.__defineGetter__("name", function(){ return _name; }); this.__defineSetter__("name", function(v){ //[snip] argument validation _name = v; }); this.__defineGetter__("value", function(){ return _value; }); this.__defineSetter__("value", function(v){ //[snip] argument validation _value= v; }); this.toString = function(){ return _name + ":" + _value"; } } ---------------------------------------------------------------- This approach can obviously leads to a potentially large amount of memory consumption. One workaround I've discovered to solve this issue is a pattern like the following: ---------------------------------------------------------------- var Foo = (function(){ var _refs = [], _props = [], _private = { get : function(o,n){ var i = _refs.indexOf(o); return ~i ? _props[i][n] : undefined; }, set : function(o,n,v){ var i = _refs.indexOf(o); if(~i) _props[i][n] = v; var len = _refs.length; _refs[len] = o; _props[len] = {}; _props[len][n] = v; } }; function Foo(name, value){ _private.set(this, "name", name); _private.set(this, "value", value); } Foo.prototype = { get name(){ return _private.get(this, "name"); }, set name(v){ _private.set(this, "name", v); }, get value(){ return _private.get(this, "value"); }, set value(v){ _private.set(this, "value", v); }, toString : function(){ return _private.get(this,"name") + ":" + _private.get(this, "value") } } return Foo; })(); ---------------------------------------------------------------- The verbosity is apparent. Has there been any discussion with Object Initialisers related to this type of problem? Respectfully, Michael Haufe _______________________________________________ es-discuss mailing list es-discuss@... https://mail.mozilla.org/listinfo/es-discuss |
|
|
Re: Inherited Privileged MembersThis proliferation of little function objects is one of the things that the various class proposals are addressing.
Waldemar _______________________________________________ es-discuss mailing list es-discuss@... https://mail.mozilla.org/listinfo/es-discuss |
| Free embeddable forum powered by Nabble | Forum Help |