Created
September 14, 2012 21:50
-
-
Save bmakarand2009/3725129 to your computer and use it in GitHub Desktop.
Javascript-classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create a class Employee, with first and LastName | |
//http://jsfiddle.net/mbhatamb/d9WqV/ | |
/* Note : This is the right way to define a class in Javascript, | |
using the prototype object, which is there for each object | |
This is the right way to define a Javascript Object.. | |
*/ | |
var Employee = function(fName, lName) { | |
this.fName = fName; | |
this.lName = lName; | |
} | |
Employee.prototype.getFirstName = function() { | |
return this.fName; | |
} | |
Employee.prototype.getLastName = function() { | |
return this.lName; | |
} | |
Employee.prototype.toString = function() { | |
return this.fName + ":" + this.lName; | |
} | |
//return this; is not needed, its implicit | |
var emp1 = new Employee("john", "adam"); | |
//if a function begins with a uppercase letter, new is | |
console.log(emp1.toString()); | |
console.log(emp1.getFirstName()); | |
console.log(emp1); | |
console.log(JSON.stringify(emp1)); //to get the JSON string {"fName":"john","lName":"adam"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Concept : An object call method defined in someother objects and classes, provided they | |
make business sense. | |
How : Use call or apply method and pass your object, along with the params. | |
Difference , call method takes params as (callerObj, param1,param2) whereas | |
apply method params are (callerObj,[param1,param2,..]); | |
*/ | |
var employee = { | |
firstName: "john", | |
lastName: "johny", | |
sal : 10000, | |
getFullName: function() { | |
return this.firstName + "::" + this.lastName; | |
}, | |
getBaseSalary: function(someBase){ | |
if(this.sal == undefined) | |
return someBase+ 5000; | |
else | |
return this.sal + someBase; | |
} | |
} | |
console.log(employee.getFullName()); | |
var member = { | |
firstName: "justin", | |
lastName: "harry", | |
isMember: function() { | |
return true; | |
} | |
} | |
console.log(member.isMember()); | |
console.log(employee.getFullName.call(member)); | |
console.log(employee.getBaseSalary.apply(member,[3000])); | |
console.log(employee.getBaseSalary.call(member,3000)); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://jsfiddle.net/mbhatamb/4JYQH/ | |
//Below is the Description of Employee Object, in a simple way | |
Employee e = new Object(); | |
e.firstName = "John"; | |
e.lastName = "tester"; | |
e.getFullName = new function() { | |
return this.firstName + " " + this.lastName; | |
} | |
console.log(e.getFullName()); | |
//Below is the creation of a Employe Object, in Object literal Notation | |
Employee e1 = { | |
firstName : "john", | |
lastName : "tester", | |
getFullName : new function() { | |
return this.firstName + " " + this.lastName; | |
} | |
console.log(e1.getFullName()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create a class Employee, with first and LastName | |
//This class is like a similar to way of doing things.. | |
/* Note : this class is inefficient, since we as we know functions are objects in themselves, | |
Now, since, in the Employee constructor itself, we are creating our getFirstName(),toString() methods, each time contructor is called, these objects are recreated. | |
*/ | |
var Employee = function(fName, lName) { | |
this.fName = fName; | |
this.lName = lName; | |
this.getFirstName = function() { | |
return fName; | |
} | |
this.getLastName = function() { | |
return lName; | |
} | |
this.toString = function() { | |
return fName + ":" + lName; | |
} | |
//return this; is not needed, its implicit | |
} | |
var emp1 = new Employee("john","adam"); | |
//if a function begins with a uppercase letter, new is | |
console.log(emp1.toString()); | |
console.log(emp1.getFirstName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment