Objects Methods


    a) In arrays the order matters, in Objects it doesn't.
    b) An object can hold different types of data - as arrays, other objetcs, functions (Called methods).

    Object Literal
    var harvey = {
    firstName: 'Harvey', //This is a key-value pair.
    //firstName = key, John = value.
    //firstName if a property of the John object.
    lastName: 'Specter',
    birthYear: 1990,
    family: ['Jessica', 'Donna', 'Mike'],
    job: 'lawyer',
    isMarried: false
    };

    //Acessar determinados elementos
    console.log(harvey.firstName);
    console.log(harvey['lastName']);
    var x = 'birthYear';
    console.log(harvey[harvey[x]]);

    //Alterando dados do objeto
    harvey.job = 'judge';
    harvey['isMarrired'] = true;

    new Object syntax
    var jessica = new Object();
    jessica.firstName = 'Jessica';
    jessica.lastName = 'Pearson';
    jessica.birthYear = 1969;
    jessica.family = ['Harvey', 'Mike', 'Donna', 'Rachel', 'Louis'];

    Métodos (Object Methods)
    var jessica = { //cria o objeto.
    firstName: 'Jessica',
    lastName: 'Pearson',
    birthYear: 1969,
    family: ['Harvey', 'Donna', 'Mike', 'Louis', 'Rachel'],
    job: 'lawyer',
    isMarried: false,
    calcAge: function(){
    this.age = 2018 - this.birthYear; //this means the present object, this object (jessica).
    //this.birthYear significa jessica.birthYear, porque o objeto é Jessica, e this refere-se ao objeto.
    //this.age adiciona o resultado de 2018 - birthYear para jessica.age
    }
    };

    jessica.calcAge();
    console.log(jessica);

    Coding Examples