Last active
November 27, 2017 18:52
-
-
Save sebabelmar/fa8a7d15ec115b7ee0c5cb2d6324e019 to your computer and use it in GitHub Desktop.
This as returning value of a function invoked in 4 different ways - traditional functions
This file contains 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
// ########## Tradictional Functions ########## | |
// ########## this bound ########## | |
// 1. | |
let bFunc = function() { | |
return this | |
} | |
// 2. | |
let obj = {} | |
obj.func = bFunc; | |
// 3. | |
let Ninja = function(){ | |
this.func = bFunc; | |
} | |
// Invocations: | |
// 1. | |
console.log('this as a function: ', bFunc()) | |
// => this as a function: Window || undefined if 'use strict' | |
// 2. | |
console.log('this as a method: ', obj.func()) | |
// => this as a method: obj {func: f} | |
// 3. | |
let ninja = new Ninja(); | |
console.log(ninja.func()); | |
// => this as a method on an instance: Ninja {func: f} | |
// 4. Apply and call do work either | |
console.log('this a a given via apply: ', bFunc.apply({banana: 'Love Banana'})) | |
// => this a a given via apply: {banana: 'Love Banana'} | |
console.log('this a a given via call: ',bFunc.call({banana: 'Love Banana'})) | |
// => this a a given via call: {banana: 'Love Banana'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment