-
var a = 100; function test() { a = 99; console.log(a); //99 console.log(this.a); //100 var a; console.log(a); //99 } test();
-
(function() { var a = b = 3; })() console.log(b); //3 console.log(a); //报错, a is not defined
-
for (var i = 1; i <= 3; i++) { setTimeout(function() { console.log(i); }, 0) } //4 4 4 //改为let声明 结果 1 2 3
-
function fn(n) { console.log(n); //123 var n = 456; console.log(n); //456 } var n = 123; fn(n);
-
var fn = 123; function fn() { console.log(fn); var n = 456; console.log(fn); } fn() //fn is not a function
-
var n = 123; function f1() { console.log(n); //123 } function f2() { var n = 456; f1(); } f2(); console.log(n); //123
-
var n = 123; function f1(n) { console.log(n); //456 } function f2() { var n = 456; f1(n); } f2(); console.log(n); //123
-
var length = 10; function fn() { console.log(this.length); } var obj = { length: 5, method: function(fn) { fn(); arguments[0](); } }; obj.method(fn, 1); //10 2
-
function fn(a) { console.log(a); var a = 2; function a() {} console.log(a); } fn(1); // function a(){} 2
-
var f = true; if (f === true) { var a = 10; } function fn() { var b = 20; c = 30; } fn(); console.log(a);//10 console.log(c);//30 console.log(b);// b is not defined
-
if('a' in window) { var a = 10; } alert(a);//10
-
var a = 10; a.pro = 10; console.log(a.pro + a);//NaN var s = 'hello'; s.pro = 'world'; console.log(s.pro + s);//undefinedhello
-
function f() { console.log(this.a); //hello } var obj = { a: 2, f: f } var f2 = obj.f; var a = 'hello'; f2();
-
function f(s) { console.log(this.a, s); //2,3 return this.a + s; } var obj = { a: 2 } var f2 = function() { return f.apply(obj, arguments); //return f.call(obj, ...arguments); } var b = f2(3); console.log(b); //5
-
var a = 10; function test() { a = 100; console.log(a); //100 console.log(this.a); //10 var a; console.log(a); //100 } test();
-
function b() { console.log(a); //function a(){} var a = 10; function a() {} a = 100; console.log(a); //100 } b();
-
(function d(num) { console.log(num); //100 var num = 10; }(100))
-
(function d(num) { console.log(num); //function num(){} var num = 10; function num() {}; }(100))
-
function m() { console.log(a1); //undefined console.log(a2); //undefined console.log(b1); //undefined console.log(b2); //undefined if (false) { function b1() {} var a1 = 100 } if (true) { function b2() {} var a2 = 10; } console.log(a1); //undefined console.log(a2); //10 console.log(b1); //undefined console.log(b2); //function b2() } m();
-
(function f(num) { function num() {} console.log(num); //function num(); var num = 10; console.log(num); //10 })(100)
-
var R = (function() { var u = { a: 1, b: 2 } var r = { fn: function(k) { return u[k]; } } return r; })(); console.log(R.fn('a')); //1