![](/img/trans.png)
[英]Get the name of a prototype constructor inside the constructor itself in javascript
[英]Can javascript pass itself from inside the constructor
我怎么能这样:
var abc = (function(){
..
})();
register(abc); // outside the protected class
对此:(不在课堂外调用注册表):
function register(object){ stores the object }
var abc = (function(){
..
register(this); // inside the protected class
})();
一些背景。
master-class有一个插件的对象数组,'register'函数放置插件。 abc会是这样一个插件。 模块patern关闭后的插件。 我想将插件实例放入列表中,插件尽可能自包含。 插件外的其他功能我想删除。
我考虑过:MasterClass.plugins.abc =(function ..)但我认为这会在加载任何插件之前创建一个对MasterClass.plugins的依赖关系进行实例化。
您可以使用Function.prototype.bind()(修改此值)和var内部,如下所示:
(function(){
var fun = function(){ alert( this.toString() )}
fun.bind(fun)();
})()
在你的情况下:
(function(){
var fun = function(){ alert( this.toString() )}
register(fun);
})()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.