  | |  | protected members in AS1 | protected members in AS1
2004-02-24 - By Victor W Allen
Back So, I was playing around with private properties etc., as described here:
http://www.crockford.com/javascript/private.html
...and I thought, "Is it possible to simulate protected members? "
This is what I came up with, any thoughts? Apologies if this is
covering old ground on the list...
V
function SuperClass(value1,value2) {
var _protected = {}; // private object holds the protected members
var _self = this; // internal methods don 't have access to "this "
var _member1 = value1;
var _member2 = value2;
var _getMember1 = function() { return _member1; };
var _setMember1 = function(value1) { _member1 = value1; };
_protected.addProperty( 'member1 ',_getMember1,_setMember1);
_protected._member2 = value2;
_protected._protectedMethod = function() { return _self; }; //
protected methods need to use _self
return _protected; // ignored when creating a new SuperClass, but
not when called as super()
}
SuperClass.prototype.toString = function() { return "[ object SuperClass
] "; };
function SubClass(value1,value2) {
var _protected = super(value1,value2); // catch those protected members
this.getMember1 = function() { return _protected.member1; };
this.getMember2 = function() { return _protected._member2; };
this.publicMethod = function() { return
_protected._protectedMethod(); };
return _protected; // just SubSubClass, should get that protected
stuff too
}
SubClass.prototype = new SuperClass();
SubClass.prototype.toString = function() { return "[ object SubClass ] "; };
sub = new SubClass( "Uno ", "Dos ");
trace(sub.getMember1());
trace(sub.getMember2());
trace(sub.publicMethod());
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
Supported by Fig Leaf Software
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
Be sure to check the archives and the wiki:
http://chattyfig.figleaf.com/
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
http://chattyfig.figleaf.com/cgi-bin/ezmlm-cgi?1:mss:105028
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
To unsubscribe send a blank e-mail to:
Normal Mode: flashcoders-unsubscribe@(protected)
Digest Mode: flashcoders-digest-unsubscrive@(protected)
|
|
 |