我有以下代码,
(function(exports) {
"use strict";
var Common = function() {
this.loading = function(type){
this.type();
this.show = function(){
alert('show');
}
this.hide = function(){
alert('hide');
}
}
exports.Common = Common;
exports.Common = new Common();
}(window));
我试图访问 show()
和 hide()
就像,
Common.loading('show');
Common.loading('hide');
但是它抛出一个错误,
TypeError: this.type is not a function
最佳答案
你正在传递一个字符串,当它到达方法时它不会神奇地变成一个函数:)
您正在尝试访问属于 this
属性的方法,因此请替换
this.type();
由
this[type]();
关于javascript - 函数调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113216/