Skip to content

Instantly share code, notes, and snippets.

@junwatu
Last active August 29, 2015 14:21
Show Gist options
  • Save junwatu/eef76dbeaeefdd206508 to your computer and use it in GitHub Desktop.
Save junwatu/eef76dbeaeefdd206508 to your computer and use it in GitHub Desktop.
call() and apply() - source http://jsbin.com/fivahatiji
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<pre>Open Console</pre> &rarr; <pre>ctrl+shift+j</pre>
<script id="jsbin-javascript">
// call() and apply() method
var dragonite = { name: 'Joko Lilo'};
var dragonSlayer = function(weapon) {
this.kill = function() {
return this.name +' is killed by '+weapon+'!';
};
return this;
};
var weaponName = 'Keris NogoSosro';
// call() first arg is 'this' and second,third fourth is named parameter
var dragon = dragonSlayer.call(dragonite, weaponName);
var job = dragon.kill();
console.log(job);
var dragonSlayerLeft = function() {
var args = arguments;
this.kill = function() {
return this.name +' is killed!';
};
this.fight = function() {
for(var i = 0; i < args.length;i++){
console.log(args[i]);
}
};
return this;
};
var weaponNames = ["Keris NogoSosro", "Amusakti", "Tombak Polowijo"];
// apply() just look like call() but the second parameter is ARRAY.
var dragonLeft = dragonSlayerLeft.apply(dragonite, weaponNames);
var job = dragonLeft.kill();
console.log(job);
dragonLeft.fight();
</script>
<script id="jsbin-source-javascript" type="text/javascript">// call() and apply() method
var dragonite = { name: 'Joko Lilo'};
var dragonSlayer = function(weapon) {
this.kill = function() {
return this.name +' is killed by '+weapon+'!';
};
return this;
};
var weaponName = 'Keris NogoSosro';
// call() first arg is 'this' and second,third fourth is named parameter
var dragon = dragonSlayer.call(dragonite, weaponName);
var job = dragon.kill();
console.log(job);
var dragonSlayerLeft = function() {
var args = arguments;
this.kill = function() {
return this.name +' is killed!';
};
this.fight = function() {
for(var i = 0; i < args.length;i++){
console.log(args[i]);
}
};
return this;
};
var weaponNames = ["Keris NogoSosro", "Amusakti", "Tombak Polowijo"];
// apply() just look like call() but the second parameter is ARRAY.
var dragonLeft = dragonSlayerLeft.apply(dragonite, weaponNames);
var job = dragonLeft.kill();
console.log(job);
dragonLeft.fight();
</script></body>
</html>
// call() and apply() method
var dragonite = { name: 'Joko Lilo'};
var dragonSlayer = function(weapon) {
this.kill = function() {
return this.name +' is killed by '+weapon+'!';
};
return this;
};
var weaponName = 'Keris NogoSosro';
// call() first arg is 'this' and second,third fourth is named parameter
var dragon = dragonSlayer.call(dragonite, weaponName);
var job = dragon.kill();
console.log(job);
var dragonSlayerLeft = function() {
var args = arguments;
this.kill = function() {
return this.name +' is killed!';
};
this.fight = function() {
for(var i = 0; i < args.length;i++){
console.log(args[i]);
}
};
return this;
};
var weaponNames = ["Keris NogoSosro", "Amusakti", "Tombak Polowijo"];
// apply() just look like call() but the second parameter is ARRAY.
var dragonLeft = dragonSlayerLeft.apply(dragonite, weaponNames);
var job = dragonLeft.kill();
console.log(job);
dragonLeft.fight();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment