Javascript, getting away from 'this' and using local scope -
in typical js class, calls member functions must preceded this
. looking @ technique let me create library of inter-dependent static functions , relying on closure/scope make things bit easier.
example:
var singleton={ //main entry point // call fn name, args... call:function(){ var args=[]; if (arguments.length==0) { return; } // fn name var fn=arguments[0]; var x; // make args array (x=1;x<arguments.length;x++) { args[args.length]=arguments[x]; } // want rid of part // see below wish // here have access fns below due hoisting in js // put them in map... var fns={ test:test // etc, more not want type/maintain } // ... can // function. var fun=fns[fn]; // instead of that, "override whitespace" , // like: // var fun=['fn_name']; // can index local scope , fn // // log error if not found if (typeof fun=='undefined') { loge('singleton: function not found:'+fn); return; } // ok, run function return fun.apply(window,args); // test fn accesses test2() without dot notation function test(a){ // note: here in test fn can access test2() // without using this.test2() syntax // in normal objects var s=test2(); alert(s+' test:'+a); }; function test2(){ return 'test2'; }; } }
i hoping more familiar advances in javascript might have advice on how emulate "implied unnecessary this
", struck me strange this
defaults window, , wouldn't nice if this
pointed anonymous object local scope attached.
i love ['localobject']
in scope.
edit:
after seeing of responses, restate in form of challenge:
what looking syntax cheat, way to, @varuna put it, "1. access static methods without using variable i.e. remain global 1 another. 2. not want maintain local array static methods , want achieve in local scope itself."
put differently, need have declared functions register themselves, don't want state function name more once. suppose @varuna has solution using eval
access local scope.
the following approach wont work:
var o={}; o['fn']=function fn(){}; o['fn2']=function fn2(){};
...because have state fn name twice, closure preserved.
and this:
var a=[ function fn(){} ,function fn2(){} ]; register(a);
will not work because, afaik, lose closure, ie. fn2 cannot see fn. makes following declarative style "this
nightmare":
window.mine={ fn:function fn(){ //this? // want fn2(), not this.fn2(), nor mine.fn2() } ,fn2:function fn2(){ //this? } ,deeper:{ //more } };
but might work, if created weird property registration on assignment:
var registar=new registar(); registar.reg=function fn(){}; registar.reg=function fn2(){}; //then access var fn=registar.getfn(n); // or var fn=registar._[n];
the above relies on js properties , having access fn.name, not available in cases afaik.
if understand correctly, want create objects that:
- have static members
- ... can accessed without using notation
the easiest solution (assuming i've understood query), use closure store stratic fields, access them directly name, explicitly add them object members.
consider:
var myconstructor = (function(){ var foo = 'somestaticfield'; var bar = function(){ alert('a static method returns ' + foo); }; return function(){ return { foo : foo, bar : bar }; }; })(); var myinstance = new myconstructor();
Comments
Post a Comment