Variable, function declaration, variable scope
1.
console.log(x) // function x () {}
var x = 1
function x() {} var x;function x () {}; console.log(x); x = 1
Copy the code
2,
var a = 5
function todo() {var a = 9 //return function () {
a = 7
}
}
todo()()
console.log(a) // 5
Copy the code
var a = 5
function todo() {a = 9} //return function () {
a = 7
}
}
todo()()
console.log(a) // 7
Copy the code
3,
var n = 3
function getn () {
return n
}
var getn2 = (function(){
n = 4
var n
n = n*2
return getn
function getn () {
return n
}
})()
console.log(getn2()) // 8
console.log(getn()) // 3
Copy the code
var n = 3
function getn () {
return n
}
var getn2 = (function(){
n = 4
n = n*2
return getn
function getn () {
return n
}
})()
console.log(getn2()) // 8
console.log(getn()) // 8
Copy the code
var n = 3
function getn () {
return n
}
var getn2 = (function(){
n = 4
var n
n = n*2
return getn
})()
console.log(getn2()) // 3
console.log(getn()) // 3
Copy the code
This points to the
n = 1
var obj = {
n: 4,
dbl: (function(){
this.n *= 2
return function () {
this.n *= 2
}
})()
}
var dbl = obj.dbl
dbl()
obj.dbl()
console.log(n, obj.n) // 4 8
Copy the code
New ()
Topic link
function Foo () {
getName = function () { console.log('1'} getName = getName = getName = getName = getName = getName = getName = getName = getName = getName = getName = getName =function () { console.log('4'} is assigned tofunction () { console.log('1'}), if not found, go to the window object, if not found in the window object create a] // 【this.getName =function () { console.log('6'} [example attribute] if this line is not annotated, the last two lines can be found here.returnThis / / 【 constructor has no return value | | return to basic types, the actual return to instantiate objects; returns a reference type - > is the actual return this reference type]} Foo getName =function () { console.log('2'} // Foo. Prototype.getname =function () { console.log('3'} // var getName =function () { console.log('4')}function getName() { console.log('5'} foo.getName () // 2 access object property getName() // 4 equivalent to: var getName;function getName() { console.log('5')}; getName =function () { console.log('4')}; Foo().getName() // 1 Foo() returns this to window, so it is equivalent to: window.getName(), where getName has been assigned tofunction () { console.log('1'} getName() // 1 equivalent to: window.getName() new foo.getName () // 2 equivalent to: new (foo.getName)() new Foo().getName() // 3 equivalent to: (new Foo()).getName(), this returned by new Foo() refers to the instance object. New new Foo().getName() // 3 = new ((new Foo()).getName)(), = getName(), = getName();function () { console.log('3'}) as a constructorCopy the code