函数调用不起作用

时间:2016-05-09 10:16:46

标签: javascript function

我有下面的代码,

(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不是函数

1 个答案:

答案 0 :(得分:4)

你正在传递一个字符串,当它到达方法时它不会神奇地成为一个函数:)

您正在尝试访问this属性的方法,因此请替换

this.type();

通过

this[type]();
相关问题