首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以[0][0]结尾的函数调用

问以[0][0]结尾的函数调用
EN

Stack Overflow用户
提问于 2017-07-19 21:03:44
回答 3查看 41关注 0票数 0
代码语言:javascript
复制
var sensor = _this.createSensor("svg", mouse, "sensor_" + timestamp.substring(2, timestamp.length - 2))[0][0];

,有人能告诉我函数调用的末尾是什么吗?

下面是我的函数的标题:

代码语言:javascript
复制
DragSensorController.prototype.createSensor = function (parent, startPoint, id, ip, typeid, timestamp) {
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-19 21:06:18

看来:

代码语言:javascript
复制
_this.createSensor("svg", mouse, "sensor_" + timestamp.substring(2, timestamp.length - 2))

返回数组的数组,如下所示:

代码语言:javascript
复制
[['foo']]

要访问它,您需要使用[0][0]。

​

代码语言:javascript
复制
const data = [['foo']];

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

​

票数 1
EN

Stack Overflow用户

发布于 2017-07-19 21:05:24

createSensor函数可能返回一个数组。例如:

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

从本质上说,它是从第一个数组中选择第一个项。在本例中,item1

票数 1
EN

Stack Overflow用户

发布于 2017-07-19 21:05:29

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

​

代码语言:javascript
复制
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

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45200959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档