Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Created August 15, 2019 09:13
Show Gist options
  • Save dotherightthing/e58c62dbb5a0749311fdc8e70cfe14c4 to your computer and use it in GitHub Desktop.
Save dotherightthing/e58c62dbb5a0749311fdc8e70cfe14c4 to your computer and use it in GitHub Desktop.
[Object literals] Understanding and viewing Object literals. #javascript

Object literals

Created: 2017.04.13

True 'Object Literal' syntax

True 'Object Literal' syntax involves creating the object properties as you create the object.

A property can be any valid JavaScript type (string, number, boolean, function, array, etc.).

A method is essentially a property of the object to which you have assigned an anonymous function. So the property is now equal to a function. It is still a property of the object, but because it 'does' something, we now call it a 'method'.

var myObj = {
property1: 'foo',
property2: 'bar',
property3_is_a_method: function() {
console.log('hello world');
}
};
console.log(myObj);

Alternatively

Alternatively, you can assign an empty object to a variable, and then assign properties to the object:

var myObj = {};
myObj.property1 = 'foo';
myObj.property2 = 'bar';
myObj.property3_is_a_method = function() {
console.log('hello world');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment