Created
May 6, 2019 10:41
-
-
Save alik472/5cc720fac13892843f29e6a4030e1571 to your computer and use it in GitHub Desktop.
JS Days - Part 2 - this in different contexts
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 b(){ | |
var myVar='b' | |
// console.log(this) | |
console.log(this.myVar,'this.myVar') | |
console.log(myVar,'myVar') | |
} | |
var myVar='a' | |
console.log(this.myVar,'this.myVar') | |
b() | |
function Person(name,age){ | |
this.name=name | |
this.age=age | |
} | |
p1=new Person('ali',19) | |
console.log(p1.myVar) | |
// console.log(this) | |
console.log('---') | |
var user={ | |
name:'ali', | |
goGlobal: function(){ | |
console.log(this===global,'Inside Object Literal') | |
console.log(this.name) | |
} | |
} | |
// inside object literal and constructor function | |
// this is not equal to global | |
// So the value of “this” depends on how a method is being invoked as well. | |
user.goGlobal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment