Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example 1
- function identify() {
- return this.name.toUpperCase();
- }
- function speak() {
- var greet = "Hi, " + identify.call(this);
- console.log(greet);
- }
- var me = {name : "Kyle"};
- var you = {name : "Jen"};
- identify.call(me); // KYLE
- identify.call(you); // JEN
- speak.call(me); // Hi, KYLE
- speak.call(you); // Hi, JEN
- // Example 2
- var add = function(x, y) {
- this.val = x + y;
- };
- var obj = {
- val: 0
- };
- add.apply(obj,[2,8]);
- // 1st, 2nd arguments
- console.log(obj, val); // 10
- add.call(obj,2,8);
- // 1st, 2nd, 3rd arguments
- console.log(obj, val); // 10
Add Comment
Please, Sign In to add comment