Creating an Object


There are two ways to creating an object.

Way 1 : Old Way

var person = new Object;
person.firstName = "Ankit";
person.lastName = "kalra";
person.getFullName = function(){
return this.firstName + " " + this.lastName; }
console.log(person.getFullName());

// Store object in var person, always remember write Object's first letter in capital.
// Object person property -- first name = Ankit
// Object person property -- last name = kalra
// Make a function for display full name
// return the parameters and we also use here Java built in function "this".

// Display On Console = Ankit kalra

Note: "this" function or keyword is Java script built in function. By using "this" function we can indicate particular id in function, i mean where we use "this" keyword there no need to implied person Object again and again, "this" keyword own self symbol of indication. It can be use for saving time and code efficiency.


Way 2 : New Way

var person = {
firstName : "Ankit",
lastName : "kalra",
getFullName : function() {
return this.lastName + " " + this.firstName;
}
}
console.log(person.getFullName());


// In new way just create object name with var and use { } braces.
// Person property -- first name : Ankit
// Use : colon for storing object properties.
// Make a function for display full name
// return the parameters and we also use here Java built in function "this".
// Close the function body curly braces.
// Close the Object body curly braces.
// Display On Console = Kalra Ankit


Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting