我知道这个问题在SO上已经问过好几次了,但是我梳理了大约12个问题,但没有一个能够帮助我。

  $(document).ready(function () {
  var Joe = function(){
    function introduce(petname){
      alert(petname)
    }

     return {
    introduce:introduce
    }
   }();

  var Jane = function(){
   function introduce(petname){
      console.log(petname)
    }

     return {
    introduce:introduce
    }


  }()
 }


如果我将joe单词存储在变量中,该如何调用该函数

 Joe.introduce('pluto')


假设我将单词Joe存储在变量fnc中

  fnc = "Joe";


我不想使用eval。我试过了window[fnc.introduce('pluto')]()
正如其他人所建议的那样,但这是行不通的。

最佳答案

尝试这个! =)



var Joe = function(){
  this.introduce = function(pet){
    alert(pet);
  }
}

var Jane = {
  introduce : function(pet){
    alert(pet);
  }
}

$(function(){
  new Joe().introduce('pluto');

  Jane.introduce('food');
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

09-20 10:11