Skip to content

Instantly share code, notes, and snippets.

@adixchen
Last active July 7, 2016 14:37
Show Gist options
  • Save adixchen/ed963f13f536cc1935f6c238db28da24 to your computer and use it in GitHub Desktop.
Save adixchen/ed963f13f536cc1935f6c238db28da24 to your computer and use it in GitHub Desktop.
var Person = function() {
this.canTalk = true;
};
Person.prototype.greet = function() {
if (this.canTalk) {
console.log('Hi, I am ' + this.name);
}
};
var Employee = function(name, title) {
Person.call(this);
this.name = name;
this.title = title;
};
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.greet = function() {
if (this.canTalk) {
console.log('Hi, I am ' + this.name + ', the ' + this.title);
}
};
var Customer = function(name) {
Person.call(this);
this.name = name;
};
Customer.prototype = Object.create(Person.prototype);
Customer.prototype.constructor = Customer;
var Mime = function(name) {
Person.call(this);
this.name = name;
this.canTalk = false;
};
Mime.prototype = Object.create(Person.prototype);
Mime.prototype.constructor = Mime;
var bob = new Employee('Bob', 'Builder');
var joe = new Customer('Joe');
var rg = new Employee('Red Green', 'Handyman');
var mike = new Customer('Mike');
var mime = new Mime('Mime');
bob.greet();
// Hi, I am Bob, the Builder
joe.greet();
// Hi, I am Joe
rg.greet();
// Hi, I am Red Green, the Handyman
mike.greet();
// Hi, I am Mike
mime.greet();
var Person = function (firstName) {
this.firstName = firstName;
};
Person.prototype.sayHello = function() {
console.log("Hello, I'm " + this.firstName);
};
var person1 = new Person("Alice");
var person2 = new Person("Bob");
var helloFunction = person1.sayHello;
// logs "Hello, I'm Alice"
person1.sayHello();
// logs "Hello, I'm Bob"
person2.sayHello();
// logs "Hello, I'm undefined" (or fails
// with a TypeError in strict mode)
helloFunction();
// logs true
console.log(helloFunction === person1.sayHello);
// logs true
console.log(helloFunction === Person.prototype.sayHello);
// logs "Hello, I'm Alice"
helloFunction.call(person1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment