[英]Can javascript pass itself from inside the constructor
How can i go from this: 我怎么能这样:
var abc = (function(){
..
})();
register(abc); // outside the protected class
to this: (whithout calling register outside the class): 对此:(不在课堂外调用注册表):
function register(object){ stores the object }
var abc = (function(){
..
register(this); // inside the protected class
})();
Some background. 一些背景。
A master-class has an object-array of plugins the 'register' function places the plugin. master-class有一个插件的对象数组,'register'函数放置插件。 abc would be such a plugin.
abc会是这样一个插件。 the plugins following the module patern closures.
模块patern关闭后的插件。 I would like to place the plugin instances into the list and the plugin be as selfcontained as possible.
我想将插件实例放入列表中,插件尽可能自包含。 Additional functions outside the plugin i would like to remove.
插件外的其他功能我想删除。
I considered: MasterClass.plugins.abc = (function..) but i think this creates a dependency on MasterClass.plugins to be instantiated before any plugins are loaded. 我考虑过:MasterClass.plugins.abc =(function ..)但我认为这会在加载任何插件之前创建一个对MasterClass.plugins的依赖关系进行实例化。
You can use Function.prototype.bind() (to modify the this value) and a var inside, like this: 您可以使用Function.prototype.bind()(修改此值)和var内部,如下所示:
(function(){
var fun = function(){ alert( this.toString() )}
fun.bind(fun)();
})()
In your case: 在你的情况下:
(function(){
var fun = function(){ alert( this.toString() )}
register(fun);
})()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.