Reflection and Extend

Reflection: An object can look at itslef, listen and change its properties and method

var john = {
    firstname: 'John',
    lastname: 'Doe'
}

john._proto_ = person; 

for(var prop in john); {//Loop over every member in the object
    console.log(prop + ": " + john[prop]);
}

What we get on chrome console:

firstname: John
lastname: Doe
getFullName: function(){
                return this.firstname + this.lastname;
              }

if(john.hasOwnProperty('prop')) //check if object has this property and not on the prototype so we do not get the getFullName method

Eg 2:

var jane = {
    address = '111 Main St', 
    getFormalFullName: function(){
        return this.lastname + ',' + this.firstname
    }
}

var jim = {
    getFirstName: function(){
        return firstname;
    }
}

//We can combine these objects and add them to john
_.extend(john, jane, jim);

results matching ""

    No results matching ""