This is my crack at the problem. I think this works back to es3 but I'm not sure?
// Declare objects within an anonymous function to limit access.varobjectRefs=(function(){// This is the object we want to inherit from.functionBase(param1,_protected){var_public=this;var_protected=_protected||{};var_private={};// Declare some variables_public.shared="Anyone can access this!";_protected.inherited="This is protected";_private.secretVar="Children cannot access this.";// Let's try a few functions._public.foo=function(){// We can access protected and private functions here. This would// not be possible if we attached it to the prototype.console.log(_protected.inherited);console.log(_private.secretVar);_private.secret();};_protected.bar=function(){// One thing to watch out for: private functions called after// construction do NOT have access to the object via 'this'. This is// masked by the fact that I assigned it to the '_public' var.// More reading: https://stackoverflow.com/q/20279484/3658757console.log(_public.shared);};_private.secret=function(){// The same warning in _protected.bar applies here too.console.log(_public.shared);};}// Inherits from BasefunctionDerived(param1,_protected){var_public=this;var_protected=_protected||{};var_private={};// Inherit (ready for the magic?)Base.call(this,param1,_protected);// Since we passed a reference to the "_protected" object as an argument// to the Base object, it has been attaching all of its protected// variables to it. We can now access those variables here:// Outputs "This is protected"console.log(_protected.inherited);// We can also access protected functions:_protected.bar();// We can even override protected variables and functions._protected.inherited="New value!";// We cannot access private variables belonging to Base.// This fails:// console.log(_private.secretVar);}// We don't want to allow public access to the constructors, because this// would let outside code pass in a '_protected' var. Instead, we create new// objects that accept all params minus '_protected', and inherit from the// target object.return{Base:function(param1){Base.call(this,param1);},Derived:function(param1){Derived.call(this,param1);}};}());// Assign the constructors to variables for clarity.varBase=objectRefs.Base;varDerived=objectRefs.Derived;// This is how you construct the object.varnewDerived=newDerived("param1");// Public functions are accessible.newDerived.foo();// Protected functions are not. These fail:// newDerived.bar();// newDerived.protected.bar();
Have fun! I'd like to add that in practice I'd probably not go to these length to protect the variables unless it was absolutely necessary. I'd probably do something more like:
// This is the object we want to inherit from.functionBase(param1,_){var_this=this;varshared=_||{};// Declare class variables and functions like this:this.foo="Anyone can access this!";shared.bar=function(){console.log(secret());return"Shared function accessed!";};varsecret=function(){// _this is needed to access object in private functions, otherwise leave out.return_this.foo;};}// Inherits from BasefunctionDerived(param1){// Inheritvarshared={};Base.call(this,param1,shared);// Outputs "Shared function accessed!"console.log(shared.bar());}// Exclude shared param when not inheriting.varmyBase=newBase("param1");varmyDerived=newDerived("param1");
Yo use it the same, you just have to trust people not to pass in a _ argument on construction to snatch the shared variables. I think it's a step up from defining public variables like this._myProtectedVar, but it's not technically protected (which is why I call the variable shared inside the function).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is my crack at the problem. I think this works back to es3 but I'm not sure?
Seems very promising. Now examining the code! :O
Have fun! I'd like to add that in practice I'd probably not go to these length to protect the variables unless it was absolutely necessary. I'd probably do something more like:
Yo use it the same, you just have to trust people not to pass in a
_
argument on construction to snatch the shared variables. I think it's a step up from defining public variables likethis._myProtectedVar
, but it's not technically protected (which is why I call the variableshared
inside the function).