Skip to content

Instantly share code, notes, and snippets.

@alik472
Created May 6, 2019 10:41
Show Gist options
  • Save alik472/5cc720fac13892843f29e6a4030e1571 to your computer and use it in GitHub Desktop.
Save alik472/5cc720fac13892843f29e6a4030e1571 to your computer and use it in GitHub Desktop.
JS Days - Part 2 - this in different contexts
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