function test() { var num = 1; if (true) { num = 2; alert(num); // 2 } alert(num); // 2 }
var scope="global"; function t(){ console.log(scope); // undefined,变量提高 var scope="local" console.log(scope); // local } t();
function Test() { var a = 1; this.b = 1; this.show = function() { alert('a=' + a); alert('b=' + this.b); } this.setValue = function() { a = 2; this.b++; } } var obj1 = new Test(); obj1.show(); // a=1,b=1; var obj2 = new Test(); obj2.setValue(); obj2.show(); // a=2,b=2; obj1.show(); // a=1,b=1; 上面的实例中,a是构造函数内部的一个变量,咱们在实例化obj1和obj2以后,发如今实例化对象时,obj1和obj2各有一个做用域,其中的a并非一份,而是不一样的值。相互之间的操做并不影响。这里的a至关于私有变量,对于每个对象来说,也都是不一样的。
(function() { var privateStatic = "privatestatic"; Func = function() { this.setPrivateStatic = function(value) { privateStatic = value; } this.getPrivateStatic = function() { return privateStatic; } } })(); var func1 = new Func(); var func2 = new Func(); console.log(func1.getPrivateStatic()); // privatestatic console.log(func2.getPrivateStatic()); // privatestatic console.log(func1.setPrivateStatic('changed')); console.log(func2.getPrivateStatic()); //changed
(function b() { var c = 1; a(); // c is not defined })() function d() { var c = 2; console.log(c); }; (function b() { var c = 1; d(); // 2 })()
https://www.cnblogs.com/dolph...
http://www.cnblogs.com/dolphi...
https://www.cnblogs.com/lhb25...
http://www.cnblogs.com/zxj159...
https://www.cnblogs.com/syfwh...
https://www.cnblogs.com/myyou...javascript