What makes this design in jquery?

$('.selector')[
action
]('class');
June 10th 19 at 14:19
1 answer
June 10th 19 at 14:21
Solution
This option record is used in JSwhen you need to get the key or to call a function with a variable.
Is the ability of the language itself, not a design jquery.
var foo = {
 bar: 'bar value',
};

console.log(foo.bar);
// => bar value

console['log'](foo['bar']);
// => bar value


Let us examine your example. Such a record:
var action = 'addClass';

$('.some-selector')[action]('some-class');

is equivalent to this:
$('.some-selector').addClass('some-class');

Same for objects:
var key = 'name';

var obj = {
 name: 'Tom',
 age: 24,
};

console.log(obj[key]);
// => Tom

console.log(obj.name);
// => Tom

console.log(obj[key] === obj.name);
// => true

key = 'age';

console.log(obj[key]);
// => 24


Also, there are cases when the key can only be accessed this way:
var obj = {
 'three words key': 'value',
};

console.log(obj['three key words']);

// => value
Sure! Thank you. Most importantly, it was possible to guess. I only had to think about it :) - Myrtice82 commented on June 10th 19 at 14:24

Find more questions by tags jQueryJavaScript