带参数的Javascript调用函数

时间:2014-04-24 22:25:10

标签: javascript

示例:

function MyDev() {
   this.someFunc;
   this.run = function() {
       if (typeof this.someFunc == 'function') {
          this.someFunc();
       } 
   }
}

var dev = new MyDev();
dev.someFunc = function(args1, args2,...) {
   //dosomething...
}
dev.run();

现在,当调用函数dev.run()时,如何用参数调用函数abcdef?有人可以帮帮我???

1 个答案:

答案 0 :(得分:0)

Javascript有一个名为“arguments”的关键字。 在函数中,这是一个包含传递给该函数的所有参数的数组。

    function hehey(){
        console.log(arguments);
        // The console will show an array with ['hello', 'foo', 'bar', 'world']
    }
    hehey('hello', 'foo', 'bar', 'world');

所以你可以通过从父函数发送arguments关键字来使用它,如下所示:

function MyDev() {
    this.someFunc;
    this.run = function() {
        if (typeof this.someFunc == 'function') {
            this.someFunc(arguments);
        }
    }
}

var dev = new MyDev();
dev.someFunc = function(test, test2) {
    console.log(test, test2);
}
dev.run("hello", "world");

有关.apply方法的更多信息:http://msdn.microsoft.com/en-us/library/ie/4zc42wh1(v=vs.94).aspx

相关问题