Created
January 29, 2019 13:19
-
-
Save jitendra19/767954d83ce9b9aaf7425f3cee672135 to your computer and use it in GitHub Desktop.
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
var a = { | |
m: function() { | |
console.log(this); // {m:f} | |
(function(){ | |
console.log(this); // window object | |
})(); | |
} | |
} | |
a.m(); | |
// {m:f} | |
// window | |
var b = a.m; | |
b(); | |
// window | |
//window | |
---------------------------------------- | |
function abc(param1,param2){ | |
console.log(this); | |
} | |
abc() // window | |
abc.call() // window | |
abc.call(null) // window | |
abc.call(1) // Number {1} | |
--------------------------- | |
'use strict'; | |
function abc(param1,param2){ | |
console.log(this); | |
} | |
abc() // undefined | |
abc.call() // undefined | |
--------------------------------- | |
var a = function() { | |
console.log(this) | |
} | |
a(); // window | |
var b = { | |
m: a | |
} | |
b.m(); // {m:f} | |
*Note if I bind this 'a' function with some other scope | |
var a = function() { | |
console.log(this) | |
}.bind({abc:'123'}) | |
a(); //confusion start here:it should be window, but not window it will be {abc:'123'} whatever is bind with function a definition time | |
var b = { | |
m: a | |
} | |
b.m(); //confusion start here: it should be {m:f}, but not, it will be {abc:'123'} whatever is bind with function a definition time | |
---------------------------------------------- | |
"use strict"; | |
function sayHello(last_name) { | |
console.log("Hello " + this + " " + last_name); | |
}.bind("Asim"); | |
sayHello("Hussain"); | |
// Error - as bind will return to a variable which is not captured. | |
here is solution:- | |
"use strict"; | |
var sayHello = function(last_name) { | |
console.log("Hello " + this + " " + last_name); | |
}.bind("Asim"); | |
sayHello("Hussain"); | |
------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment