Last active
November 2, 2017 05:23
-
-
Save DeoThemes/64062e9ecf45aeaff668a5ada880d5e5 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/semicibudi
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
// 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