简体   繁体   English

这个“ []”在做什么?

[英]What is this “[]” here doing?

Below is an extract from my textbook. 以下是我的教科书摘录。

I have a question about the use of [] here in window.history[type]() . 我在window.history[type]()有一个关于[]用法的问题。 I can tell that it's separating the object name(window) and the variable(type) so that they can be recognized as separate things but is there a name for this use of []? 我可以说它是将对象名称(窗口)和变量(类型)分开,以便可以将它们识别为单独的东西,但是[]的使用是否有名称? I performed a google search but nothing came up. 我执行了谷歌搜索,但没有任何反应。

$(function() {
//omitted
['back', 'forward'].forEach(function(type) {
  $('.' + type).click(function() {
    window.history[type]();
    });
  });
});

This is property/method access using bracket notation . 这是使用方括号表示法的属性/方法访问 In Javascript, you can access the properties of an object using the dot notation: 在Javascript中,您可以使用点表示法访问对象的属性:

myObj.prop

Or the bracket notation: 或括号符号:

myObj['prop']

When you dynamically construct the properties, however, you have no choice but to use bracket notation: 但是,当动态构造属性时,只能使用方括号表示法:

window.history['forward']()

is the same as 是相同的

window.history.forward()

Here you are iterating on the forward and back properties, and the bracket notation is used to call the functions from their string names on window.history . 在这里,您在forwardback属性上进行迭代,并且使用括号符号从window.history上的字符串名称中window.history函数。

Here is the doc linked by @Teemu 这是@Teemu链接的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM