Created
March 30, 2017 11:11
-
-
Save DadgadCafe/b3a1a41cb0cfc72068fa3c894df0fedf to your computer and use it in GitHub Desktop.
notes about with statement.
This file contains hidden or 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
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