Skip to content

Instantly share code, notes, and snippets.

@santhosh17s
Last active April 23, 2018 07:21
Show Gist options
  • Save santhosh17s/4c421f5205a9ffd9cfc9f4eee03782f9 to your computer and use it in GitHub Desktop.
Save santhosh17s/4c421f5205a9ffd9cfc9f4eee03782f9 to your computer and use it in GitHub Desktop.
//Simple Class function
function API() {
this.name = 'This is API name';
}
//prototype function for share between the object
API.prototype.getName = function() {
//Take copy of this keyword
var self = this;
return new Promise( function(resolve, reject){
resolve(self.name);
//resolve(this); // [Object window]
});
}
//for Fat-Arrow function with this keyword
API.prototype.getNamewithArrowFun = function() {
return new Promise( (resolve, reject) => {
resolve(this.name);
//resolve(this); // [Object window]
});
}
//create object using new keyword. It is a constructor function.
var api = new API();
api.getName().then( data => console.log(data)); //"This is API name"
api.getNamewithArrowFun().then( data => console.log(data)); //"This is API name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment