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
function price() { | |
return this.price; | |
} | |
function howMuchIsIt() { | |
var ans = "It's " + price.call( this ) + ' worth'; | |
console.log( ans ); | |
} | |
var productA = { |
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 obj = { | |
x:10, | |
printX:function(){ | |
console.log(this.x); | |
} | |
} | |
var printx1 = obj.printX; | |
printx1() // => undefined | |
var printx2 = printx1.bind(obj) |
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 indirect = { name: 'indirect function invocation' }; | |
function concatName(string) { | |
console.log(this === indirect); // => true | |
return string + this.name; | |
} | |
// Indirect invocations | |
concatName.call(indirect, 'Hello '); // >> 'Hello indirect function invocation' | |
concatName.apply(indirect, ['Bye ']); // >> 'Bye indirect function invocation' |
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
function thisAtConstructorInvoke() { | |
console.log(this instanceof thisAtConstructorInvoke); // >> true | |
this.property = "default value"; | |
} | |
var instance = new thisAtConstructorInvoke(); | |
instance.property; // >> default value |
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 methodInvoke = { | |
des : "calling function owned by object", | |
mean : function() { | |
console.log(this===window); | |
console.log('method invocation is a ' + this.des); | |
} | |
} | |
var seperated_method = methodInvoke.mean | |
seperated_method() // >> true, 'method invocation is a undefined' |
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 methodInvoke = { | |
des : "calling function owned by object", | |
mean : function() { | |
console.log(this===methodInvoke); | |
console.log('method invocation is a ' + this.des); | |
} | |
} | |
methodInvoke.mean(); // >> true, method invocation is a calling function owned by object |
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
function thisAtfunctionInvoke(){ | |
console.log(this===window); | |
this.number = 20 | |
} | |
thisAtFunctionInvoke(); // >> true | |
console.log(window.number===20); // >> true | |
console.log(this === window); // >> true |
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
function thisAtfunctionInvokeInStrictMode(){ | |
'use strict'; | |
console.log(this===undefined); | |
this.number = 20 | |
} | |
thisAtfunctionInvokeInStrictMode(); // >> true | |
console.log(window.number===20); // >> false |