Javascript objects

, 25/09/2017

Real Life Objects, Properties, and Methods

In real life, a car is an object. A car has properties like weight and color, and methods like car.start() and car.stop().

Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:

var car = {type:"Fiat", model:"500", color:"white"};

Methods are actions that can be performed on objects.
Methods are stored in properties as function definitions.

Create an object

var person = {

Accessing Object Properties

objectName.propertyName // person.lastname or
objectName["propertyName"] // person["lastname"]

Using an object constructor

function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");

Deleting Properties

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
delete person.age;   // or delete person["age"];