Skip to content

Instantly share code, notes, and snippets.

@DadgadCafe
Created March 30, 2017 11:11
Show Gist options
  • Save DadgadCafe/b3a1a41cb0cfc72068fa3c894df0fedf to your computer and use it in GitHub Desktop.
Save DadgadCafe/b3a1a41cb0cfc72068fa3c894df0fedf to your computer and use it in GitHub Desktop.
notes about with statement.
const obj = {
x: 10,
foo: function foo () {
function bar () {
console.log(x) // undefined, scope foo
console.log(y) // 30, scope bar
console.log(this.x) // 20
console.log(this.y) // undefined
}
with (this) {
// let x; x = 20 // will not change this.x
var x = 20 // declare x in scope foo, this.x === obj.x
var y = 30 // declare y in scope foo, y = 30
bar.call(this) // this === obj
}
}
}
obj.foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment