Quick actions

cmd+k|ctrl+k

Navigation

Languages

Apply & Call

Snippet info

Language

JavaScript

Visibility

public

Author

nvmarcosalex

Created

2020-04-08T16:59:17Z

Updated

2020-10-21T17:18:48Z

function speak(line) {
    console.log("The " + this.type + " rabbit says '" + line + "'" );
}

var whiteRabbit = {type: "gray", speak: speak};


// Simple object method
whiteRabbit.speak("Oh my god, how late it's getting");

// apply & call
// apply & bind can be used of object.method()
// apply & bind methods both take a first argument that can be used to simulate method calls
// The first argument is in fact used to give a value to this

speak.apply(whiteRabbit, ["Burp"]);
speak.call({type: "old"}, "Oh my!");
INFO