2017-07-19 31 views
0
var sensor = _this.createSensor("svg", mouse, "sensor_" + timestamp.substring(2, timestamp.length - 2))[0][0]; 

有人可以告诉我函数调用结尾处的[0] [0]是用于什么吗?以[0] [0]结尾的函数调用

这是我的函数的标头:

DragSensorController.prototype.createSensor = function (parent, startPoint, id, ip, typeid, timestamp) { 
+1

2D阵列存取? – csmckelvey

回答

1

createSensor功能可能返回的数组的数组。例如:

[['item1', 'item2'], ['something1']]

所以基本上它是选择从所述第一阵列中的第一项。在这种情况下,item1

1

这意味着该函数返回一个数组,并且第一个[0]获得对第一个数组元素的引用。然后,该元素本身就是一个数组,所以第二个[0]获取该数组的第一个元素。

function foo(){ 
 
    var a = [[1,2,3], [4,5,6]]; 
 
    
 
    // Calling this function will return an array consisting of two arrays 
 
    return a; 
 
} 
 

 
// Run the function and get the first item from the first array 
 
console.log(foo()[0][0]); // 1 
 

 
// Run the function and get the 3rd item from the second array 
 
console.log(foo()[1][2]); // 6

+0

或许,说一个类似数组的对象可能更为正确。 – Amy

+0

@Amy我们并不知道,但为了这个问题的目的,这并不重要。 –

1

看来:

_this.createSensor("svg", mouse, "sensor_" + timestamp.substring(2, timestamp.length - 2)) 

返回数组的像一个数组:

[['foo']] 

访问它,你需要使用[0][0]

const data = [['foo']]; 
 

 
console.log(data[0][0]);