在我的对象init中,我用他们的名字调用方法。但有些时候,这些方法可能没有声明,或者我不想给它们打电话。如果有机会,如何防止我的方法被调用?如何防止方法调用,当不存在
这里是我的调用方法:this[collectionName]();
- 这里的名称是我收到的参数。所以该方法在对象内声明。
这里是全码:
init: function(collectionName, size){
if((typeof this[collectionName]) === undefined) return; //but not works!!!
this.collectionName = collectionName;
this.size = size.toUpperCase() == "SMALL" ? 20 : size.toUpperCase() == "MEDIUM" ? 35 : lsize.toUpperCase() == "LARGE" ? 50 : "SMALL";
this[collectionName]();//some time method will not exist. how to check the existence and prevent it from call?
return this.generateRecords();
}
我得到一个错误,当该方法是不是他们那么:
New-DataModels.js?bust=1491457640410:69 Uncaught TypeError: this[collectionName] is not a function
您可以检查'这个[集合名]'exista,如果是一个函数 – Satpal
(typeof运算这个[集合名] ===“功能”)? –
这就是我写的一个条件,但不适合我。查看我的commet – 3gwebtrain