When to use “:”(colon) operator in javascript vs “=” operator?

前端 未结 3 377
南旧
南旧 2021-01-04 19:31

I tried looking online everywhere for past hour, but I can\'t seem to figure out when to use colon operator : vs = operator in javascript? From wh

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 19:59

    The colon(:) operator as you correctly stated, is used to define an object property:

    var object = {
      property:value
    }
    

    The equals(=) operator is used to assign values to something, a variable, an array etc.

    If you only defined your object as:

    var object = {}
    

    You could assign properties to it like this:

    object.property = value;
    

提交回复
热议问题