javascript - Call function so this not needed -
i have application uses v8 javascript engine, , within add functions namespace objects execute lines of code database. contents of these functions need not have this
added before every function call. following example code of problem
var obj = {}; obj.method = function(a) { return a; } obj.executor = function() { return method(5); } obj.executor() referenceerror: method not defined var caller = function() { return method(5); } caller.call(obj) referenceerror: method not defined
as can see, neither way allows me call method
without first adding this
. there way of executing function it's context set in such way this
not need added?
edit
this did work in previous version of v8 engine, seems recent 1 not allowing now.
"the client's write rules strings loaded database, , requirement (who knows why) need write function names , application sorts out scoping."
if you're not running in strict mode, can use with
statement.
var obj = {}; obj.method = function(a) { return a; }; obj.executor = function() { (this) { return method(5); } }; obj.executor();
var caller = function() { (this) { return method(5); } }; caller.call(obj);
not saying great solution, it'll work if requirements given.
i don't know other requirements, can achieve via closure well.
var obj = {}; (function() { var method = obj.method = function(a) { return a; }; obj.executor = function() { return method(5); }; }(); obj.executor();
Comments
Post a Comment