Reputation: 113
Below is an extract from my textbook.
I have a question about the use of [] here in 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]();
});
});
});
Upvotes: 2
Views: 132
Reputation: 13963
This is property/method access using bracket notation. In Javascript, you can access the properties of an object using the dot notation:
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
.
Here is the doc linked by @Teemu
Upvotes: 5