1
我需要能夠在運行時在Dojo中實例化類的對象,並將其混合到另一個對象中(有點像在Java中指定extends
或implements
,但是在運行時)。我想出了以下解決方案:像這樣實例化dojo類有什麼內在的危險嗎?
var declaredClassBackup = this.declaredClass; // backup the "declaredClass"
var mixinObject = null;
try {
dojo.require(kwArgs.mixinClassName);
/*
* Eval the mixinClassName variable to get the Function reference,
* then call it as a constructor with our mixinSettings
*/
mixinObject = new (eval(kwArgs.mixinClassName))(kwArgs.mixinSettings);
} catch (e){
if(console){
console.error("%s could not be loaded as a mixin.",
kwArgs.mixinClassName);
}
mixinObject = new package.path.DefaultMixin(kwArgs.mixinSettings);
}
dojo.mixin(this, mixinObject);
/*
* Re-set the declaredClass name back to that of this class.
*/
this.declaredClass = declaredClassBackup;
這種類型的代碼有什麼問題,如果有的話? (你會如何使它更強大?)另外,有沒有什麼我可以錯過的道場,這會爲我更優雅地做到這一點?
什麼定義'kwArgs.mixinClassName'? – 2009-11-16 18:38:01
kwArgs是傳遞給包含上述代碼的函數的對象...該函數的調用者是誰定義kwArgs.mixinClassName的值 – JasonWyatt 2009-11-16 18:49:19