Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:数组[]后面加括号()是什么意思?_Javascript - Fatal编程技术网

JavaScript:数组[]后面加括号()是什么意思?

JavaScript:数组[]后面加括号()是什么意思?,javascript,Javascript,我是JS新手,我读过一段包含这行代码的代码 this.myArray[index](this._sender, args); 我想知道它是什么意思?这意味着myArray[index]是myArray的元素,它是函数,这就是为什么它需要两个参数。这意味着这个数组项是函数,它是用参数this调用的。_sender和args 在本例中,我声明了一个数组,将一个单参数函数推送到它,然后调用它 var arr=[]; arr.push(功能(str){ document.body.innerHTML

我是JS新手,我读过一段包含这行代码的代码

this.myArray[index](this._sender, args);

我想知道它是什么意思?

这意味着myArray[index]是myArray的元素,它是函数,这就是为什么它需要两个参数。

这意味着这个数组项是函数,它是用参数
this调用的。_sender
args

在本例中,我声明了一个数组,将一个单参数函数推送到它,然后调用它

var arr=[];
arr.push(功能(str){
document.body.innerHTML=“嘿,”+str;
});

arr[0](“您的姓名”)在JavaScript中,函数是第一类对象,因此它们可以像任何其他值一样被传递/引用、存储和访问

var myArray = [];
var myFunction = function(name) {
  console.log("Hello, " + name + "!");
};

myFunction('World');

myArray[0] = myFunction;
myArray[0]('again');

function addDefault(func, defaultValue) {
  return function(name) {
    name = name ? name : defaultValue;

    func(name);
  }
}
var myFunctionWithDefault = addDefault(myFunction, 'stranger');
myFunctionWithDefault();
myFunctionWithDefault('goodbye');

JSBin:

它类似于以下代码片段:

var myClass = function () {
    this._a = 5;
    var index = arguments[0] || 0;

    this.myArray = [
        function (a, b) {
            console.log(a, b);
        },
        function (a, b) {
            console.log(a, b);
        }
    ];
    this.myArray[index](this._a, arguments);
};

var obj = new myClass(1, 2, 3);//5  [1, 2, 3]

this.myArray
可能包含函数引用,因此
this.myArray[index]
获取函数引用,然后使用参数调用它。该参数看起来像是数组值用于触发函数,因此参数“this.\u sender”“args”
(this.\u sender,args)