Last active
February 8, 2019 04:35
-
-
Save dlucidone/e270e9433bcd2be7598d158b8c339374 to your computer and use it in GitHub Desktop.
JS Bind Implementation
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
Function.prototype.myCall = function(){ | |
return this.bind(...arguments)(); | |
} | |
ex- | |
function showProfileMessage(message) { | |
console.log(message, this.name); | |
} | |
const obj = { | |
name: "Ankur Anand" | |
}; | |
showProfileMessage.myCall(obj, "welcome "); | |
ex- | |
function Product(name, price) { | |
this.name = name; | |
this.price = price; | |
} | |
function Food(name, price) { | |
Product.myCall(this, name, price); | |
this.category = 'food'; | |
} | |
console.log(new Food('cheese', 5).name); | |
ex- | |
var sData = 'Wisen'; | |
function display() { | |
console.log('sData value is %s ', this.sData); | |
} | |
display.myCall(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment