Skip to content

Instantly share code, notes, and snippets.

@DeoThemes
Last active November 2, 2017 05:23
Show Gist options
  • Save DeoThemes/64062e9ecf45aeaff668a5ada880d5e5 to your computer and use it in GitHub Desktop.
Save DeoThemes/64062e9ecf45aeaff668a5ada880d5e5 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/semicibudi
// Literal notation
var person = {
name: "Alex",
age: 29,
greet: function() {
console.log("Hello, I'm " + this.name);
}
}
Object.prototype.greet = function() {
console.log("Hello there");
}
var alex = Object.create(person);
var anna = Object.create(person);
anna.name = "Anna";
console.log(anna.name);
anna.greet();
// Constructor pattern
function Fruit( theColor, theFruitName ) {
this.color = theColor;
this.fruitName = theFruitName;
this.showName = function() {
console.log("This is a " + this.fruitName);
}
}
var mangoFruit = new Fruit("Yellow", "Mango");
mangoFruit.showName();
console.log(mangoFruit.hasOwnProperty("color"));
// Prototype pattern
function Vegetable() {
}
Vegetable.prototype.color = "Red";
Vegetable.prototype.wegetableName = "Cucumber";
Vegetable.prototype.showName = function() {
console.log("This is a " + this.wegetableName);
}
var appleFruit = new Vegetable();
appleFruit.showName();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment