This
bind,apply,call
by harps116
HTML
<!-- logging outside the console -->
<pre><script>
function log(arg){
document.writeln(arg);
document.write('<br/>')
}
</script></pre>
JavaScript
function logThis() {
log(`name: ${this.name}`);
log(`invokedBy: ${this.invokedBy}`);
log(`---------------------------------------`);
}
var setThis = logThis.bind({name: 'bindThis', invokedBy: 'functionCall'});
setThis();
var applyThis = logThis.apply({name: 'applyThis', invokedBy: 'apply'});
var callThis = logThis.call({name: 'callThis', invokedBy: 'call'});
var applyTwo = logThis.apply({name: 'applyTwo', invokedBy: 'apply'});
var callTwo = logThis.call({name: 'callTwo', invokedBy: 'call'});
// note that setThis is permantly set and cannot be changed as bind was used.
log(`^^cannot be changed^^`);
setThis.apply('this is pointless');