Created
January 29, 2019 13:20
-
-
Save jitendra19/90cd87036f644151e5a2c8dc76097243 to your computer and use it in GitHub Desktop.
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
// construction pattern | |
function abc(a,b){ | |
this.a=a; | |
this.b=b; | |
} | |
var obj = new abc('123','456') | |
obj // {a:'123', b":'456'} | |
similarly we can achieve like this: | |
var obj1 = {} | |
abc.call(obj1, 'xyz', 'zzz'); | |
obj1 // {a:'xyz', b:'zzz'} | |
-------------------------------------------------- | |
// constructor pattern and closure example:- | |
function Device(kind) { | |
this.kind = kind; | |
this.printKind = function () { | |
console.log(kind); | |
console.log(this.kind); | |
} | |
} | |
var product = new Device("music player"); | |
product.kind = "radio"; | |
product.printKind(); | |
// music player | |
// radio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment