Skip to content

Instantly share code, notes, and snippets.

@JohnnyMa
Created July 20, 2015 09:10
Show Gist options
  • Save JohnnyMa/4fa19265d30955d586b1 to your computer and use it in GitHub Desktop.
Save JohnnyMa/4fa19265d30955d586b1 to your computer and use it in GitHub Desktop.
Binding Methods to Objects
//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