![](/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.