Unfamiliar use of square brackets in calling a function

前端 未结 5 2203
滥情空心
滥情空心 2021-02-19 23:41

In the middle of this page, I find the code below.

var plus = function(x,y){ return x + y };
var minus = function(x,y){ return x - y };

var operations = {
  \'+         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 23:49

    operations is an object and when you do operations[property] you will get the associated function and then you are passing the operands as x and y.

    operations['+'] is function (x,y){ return x + y } which is plus

    operations['-'] is function (x,y){ return x - y } which is minus

提交回复
热议问题