Created
October 4, 2013 20:42
-
-
Save sjgjohnston/6832398 to your computer and use it in GitHub Desktop.
Mini lab on Javascript prototyping...
This file contains 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
MINI LAB: | |
Do a Person class and create two subclasses "Manager" and "Employee" | |
First define constructors, then define prototypes | |
1. Define the person function: | |
Person = function (name) { | |
if(name) { | |
this.name = name; | |
} | |
} | |
2. Create the object constructor - define the Person prototype... | |
Person.prototype = { | |
manager: false, | |
employeeType: '(not set)', | |
companyName: '(not set)', | |
introduce: function() { | |
if (this.isManager()) { | |
this.employeeType = "a manager"; | |
} else { | |
this.employeeType = "an employee"; | |
} | |
console.log("My name is: " + this.name + ". I am " + this.employeeType + " at " + this.companyName) | |
}, | |
getCompanyName: function() { | |
return !!this.companyName; | |
}, | |
getEmployeeType: function() { | |
return !!this.employeeType; | |
}, | |
isManager: function() { | |
return !!this.manager; | |
} | |
} | |
3. Create a Person object | |
> kyle = new Person('Kyle') | |
Person {name: "Kyle", manager: false, employeeType: "(not set)", companyName: "(not set)", introduce: function…} | |
4. Test the properties/attributes of the object | |
> kyle.introduce() | |
My name is: Kyle. I am an employee at (not set) | |
undefined | |
> kyle.isManager() | |
false | |
> kyle.hasOwnProperty('manager') | |
false | |
> console.log(kyle.isManager()) | |
false | |
undefined | |
5. Set the properties/attributes of the object | |
> kyle.manager = true; | |
true | |
> kyle.introduce() | |
My name is: Kyle. I am a manager at (not set) | |
undefined | |
> kyle.employeeType = 'contractor'; | |
"contractor" | |
> kyle.introduce() | |
My name is: Kyle. I am a manager at (not set) | |
undefined | |
> kyle.companyName = 'Twitter, Inc.' | |
"Twitter, Inc." | |
> kyle.introduce() | |
My name is: Kyle. I am a manager at Twitter, Inc. | |
undefined | |
> kyle.manager = false; | |
false | |
> kyle.introduce() | |
My name is: Kyle. I am an employee at Twitter, Inc. | |
undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment