Javascript

JS – Bind example

window.name = "Jane Doe";

var globalGreet = function() {
    return "My name is " + this.name;
};

var johnDoe = {
    name: "John Doe",
    greet: window.globalGreet.bind(johnDoe)
};

globalGreet(); // "My name is Jane Doe"

johnDoe.greet(); // "My name is John Doe"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s