JavaScript Variable fallback

前端 未结 4 1888
不知归路
不知归路 2021-02-13 15:28

Please can someone explain to me what this line of code does:

var list  = calls[ev] || (calls[ev] = {});

My best guess:

It\'s setting t

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-13 16:04

    you are right about your first guess. This is a common pattern for initialising javascript namespaces. It serves to make sure you dont overwrite a previous object with the same name. Most popular libraries will do something similar in order to create their namespace objects.

    The parenthesis is there so that the expressions are evaluated in the proper order.

提交回复
热议问题