[~/Dropbox/src/javascript/learning]$ cat call.js var Bob = { name: "Bob", greet: function() { console.log("Hi, I'm " + this.name); } } var Alice = { name: "Alice", }; Bob.greet.call(Alice);
[~/Dropbox/src/javascript/learning]$ node call.js Hi, I'm Alice
> function f() { console.log(this.x); } undefined > f.toString() 'function f() { console.log(this.x); }' > z = { x : 99 } { x: 99 } > f.call(z) 99 undefined >
> o = { x : 15 } { x: 15 } > function f(m) { console.log(m+" "+this.x); } undefined > f("invoking f") invoking f 10 undefined > f.call(o, "invoking f via call"); invoking f via call 15 undefined
Casiano Rodríguez León