-
-
Save Bijesse/2186e9dc20577d6e5c62b4b0c6674e2a to your computer and use it in GitHub Desktop.
call, apply and bind with this // source http://jsbin.com/pihiye
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>call, apply and bind with this </title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// With a partner, please complete the exercises below: | |
var potus = { | |
name: 'Barack Obama' | |
} | |
var candidates = { | |
democrat: { | |
name: 'Hillary Clinton', | |
}, | |
republican: { | |
name: 'Donald Trump', | |
}, | |
libertarian: { | |
name: 'Gary Johnson' | |
} | |
}; | |
function greet (greeting, punctuation) { | |
console.log(greeting + ', ' + this.name + punctuation) | |
} | |
// IMPORTANT! For the following questions, do no make any edits to the above code! | |
// 1) Use any method to print 'Hello, Barack Obama.' to the console | |
greet.call(potus,"Hello", "."); | |
// 2) Use .call to print 'Wat Up, Hillary Clinton!' to the console | |
greet.call(candidates.democrat, "Wad up", "!") | |
// 3) Use .apply to print 'Yo, Donald Trump!' to the console | |
greet.apply(candidates.republican, ["Yo!", "!"]) | |
// 4) Use .bind to print 'Who are you, Gary Johnson?' | |
var greetGary = greet.bind(candidates.libertarian); | |
greetGary("who are you", "?") | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// With a partner, please complete the exercises below: | |
var potus = { | |
name: 'Barack Obama' | |
} | |
var candidates = { | |
democrat: { | |
name: 'Hillary Clinton', | |
}, | |
republican: { | |
name: 'Donald Trump', | |
}, | |
libertarian: { | |
name: 'Gary Johnson' | |
} | |
}; | |
function greet (greeting, punctuation) { | |
console.log(greeting + ', ' + this.name + punctuation) | |
} | |
// IMPORTANT! For the following questions, do no make any edits to the above code! | |
// 1) Use any method to print 'Hello, Barack Obama.' to the console | |
greet.call(potus,"Hello", "."); | |
// 2) Use .call to print 'Wat Up, Hillary Clinton!' to the console | |
greet.call(candidates.democrat, "Wad up", "!") | |
// 3) Use .apply to print 'Yo, Donald Trump!' to the console | |
greet.apply(candidates.republican, ["Yo!", "!"]) | |
// 4) Use .bind to print 'Who are you, Gary Johnson?' | |
var greetGary = greet.bind(candidates.libertarian); | |
greetGary("who are you", "?") | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// With a partner, please complete the exercises below: | |
var potus = { | |
name: 'Barack Obama' | |
} | |
var candidates = { | |
democrat: { | |
name: 'Hillary Clinton', | |
}, | |
republican: { | |
name: 'Donald Trump', | |
}, | |
libertarian: { | |
name: 'Gary Johnson' | |
} | |
}; | |
function greet (greeting, punctuation) { | |
console.log(greeting + ', ' + this.name + punctuation) | |
} | |
// IMPORTANT! For the following questions, do no make any edits to the above code! | |
// 1) Use any method to print 'Hello, Barack Obama.' to the console | |
greet.call(potus,"Hello", "."); | |
// 2) Use .call to print 'Wat Up, Hillary Clinton!' to the console | |
greet.call(candidates.democrat, "Wad up", "!") | |
// 3) Use .apply to print 'Yo, Donald Trump!' to the console | |
greet.apply(candidates.republican, ["Yo!", "!"]) | |
// 4) Use .bind to print 'Who are you, Gary Johnson?' | |
var greetGary = greet.bind(candidates.libertarian); | |
greetGary("who are you", "?") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment