Meaning of '{ }' in javascript

后端 未结 7 674
余生分开走
余生分开走 2021-02-07 17:37

What does assigning a variable to {}, mean? Is that initializing it to a function? I have code in a javascript file that says this

GLGE.Wavefront =          


        
相关标签:
7条回答
  • 2021-02-07 18:01

    That would be an empty JavaScript object.

    0 讨论(0)
  • 2021-02-07 18:07

    It does create an empty object.

    var myObj = {};
    

    Within an object you can define key/value pairs, e.g.:

    myObj.color = 'red';
    

    A value can be a function, too:

    myObj.getColor = function() { return this.color };
    
    0 讨论(0)
  • 2021-02-07 18:08

    It's an initialized empty object, eg. an object of no particular type. It serves to provide a definition for this.materials so that the code won't have to check it for null or being defined later.

    0 讨论(0)
  • 2021-02-07 18:15

    What does assigning a variable to {}, mean?

    It is an object literal (with no properties of its own).

    Is that initializing it to a function?

    No, that would be = function () { }.

    how is that assignment different from an array?

    An array has a bunch of features not found in a basic object, such as .length and a bundle of methods.

    Objects are often used to store arbitrary key/value pairs. Arrays are for ordered values.

    0 讨论(0)
  • 2021-02-07 18:16

    Using {} creates an object. You can use the object like a hash-map or similar to how you can use arrays in PHP.

    var obj = {};
    obj['test'] = 'valuehere';
    obj.secondWay = 'secondValue';
    

    and you could access them by calling obj.test and obj['secondWay'].

    0 讨论(0)
  • 2021-02-07 18:19

    It's JON (Javascript Object Notation) for creating a new empty object. Almost equal in idea to how you'd normally do Object x = new Object() in java, minus the initialization part...

    0 讨论(0)
提交回复
热议问题