-
-
Save souparno/8614456 to your computer and use it in GitHub Desktop.
Combination Constructor & Prototype Pattern
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
/** | |
* Combination Constructor/Prototype Pattern | |
*/ | |
//constructor | |
function Something(name){ | |
//properties | |
this.name = name; | |
} | |
//prototype | |
Something.prototype = { | |
//methods | |
sayhello:function(){ | |
console.log('hello '+this.name); | |
}, | |
sayhi:function(){ | |
console.log('hi '+this.name); | |
} | |
} | |
var obj = new Something('souparno'); | |
var obj2 = new Something('zacks'); | |
/********************EOF*******************************/ | |
/** | |
* Dynamic Prototype Pattern | |
*/ | |
//constructor | |
function Something(name){ | |
//properties | |
this.name = name; | |
//methods | |
if(typeof this.sayhello != "function"){ | |
Something.prototype.sayhello = function(){ | |
console.log('hello '+this.name); | |
}; | |
} | |
if(typeof this.sayhi != "function"){ | |
Something.prototype.sayhello = function(){ | |
console.log('hi '+this.name); | |
}; | |
} | |
} | |
var obj = new Something('souparno'); | |
var obj2 = new Something('zacks'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment