Search code examples
javascriptobjectmethodspropertiesobject-literal

Object literal property and method location


Do all methods have to be stated after all properties in JavaScript when defining objects in object literal form? I have tried voiding this idea and it seems that all properties stated afterwards are not included.


Solution

  • Do all methods have to be stated after all properties in JavaScript when defining objects in object literal form?

    No.

    Object properties have a value that is either a primitive or a reference to an object. The order in which they are added, either by a literal or assignment, is irrelevant:

    var obj = {
      prop0: 0,
      method0: function(){},
      prop1: 1,
      method1: function(){}    
    }