Last active
August 29, 2015 14:21
-
-
Save junwatu/eef76dbeaeefdd206508 to your computer and use it in GitHub Desktop.
call() and apply() - source http://jsbin.com/fivahatiji
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"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<pre>Open Console</pre> → <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> |
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
// 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