Skip to content

Instantly share code, notes, and snippets.

@jitendra19
Created January 29, 2019 13:20
Show Gist options
  • Save jitendra19/1f5d2b62ee29bb5a8df95e1cfc95082e to your computer and use it in GitHub Desktop.
Save jitendra19/1f5d2b62ee29bb5a8df95e1cfc95082e to your computer and use it in GitHub Desktop.
// 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