Created
July 20, 2015 09:10
-
-
Save JohnnyMa/4fa19265d30955d586b1 to your computer and use it in GitHub Desktop.
Binding Methods to Objects
This file contains hidden or 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
//usually used in event processing | |
var obj = { | |
msg: 'Name is', | |
buildMessage: function (name) { | |
return this.msg + ' ' + name; | |
} | |
} | |
alert(obj.buildMessage('John')); // displays: Name is John | |
f = obj.buildMessage; | |
alert(f('Smith')); // displays: undefined Smith | |
g = bind(obj, obj.buildMessage); | |
alert(g('Smith')); // displays: Name is Smith | |
//original url: http://codetunnel.com/9-javascript-tips-you-may-not-know/#string-concatenation-vs-arrayjoin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment