Objects
  • Dot notation is faster to write and clearer to read.
  • Square bracket notation allows access to properties containing special characters and selection of properties using variables
  • Objects are named value pairs
var obj = {
    firstname: 'John',
    lastname: 'Carroll',
    greet: function() {
            console.log(this.firstname);
        }
}
 
console.log(obj.a) // 1
console.log(obj.b) // John
console.log(obj['a']) // 1 mostly used for when passing through the key as a variable
 
obj.greet();
 
var address = {
    no: 1,
    street: "Birch Avenue",
    address: {
           town: 'Middleton',
           city: 'Manchester'
        }
}

When a function is attached to a object it is called a method

var obj = {
    a: 1,
    b: "John",
    c: function() {
           // 
        }
}
 
console.log(obj.a) // 1
console.log(obj.b) // "John"
console.log(obj['a']) // 1
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License