var a = 100; function test(){ console.log(a); } function testFun(){ var a = 200; test(); }
不假思索的想到 出书的必定是 200啊 ,然而结结实实被打脸,输出 100函数
在编译时,this
全局做用域中会存有的对象code
- a
- test
- testFun
testFun做用域中存的对象
- a
test作用域
- 无
在运行时,字符串
test() 我这里须要变量a 啊,可是在test的做用域中并不存在,那么就须要去全局做用域中寻找 a,报告老大找到了,输出全局做用域中的a,输出100get
var a = 100; function testFun2(){ var a = 300; function test(){ console.log(a) } test(); } testFun2();
输出什么? 300!it
?!为何呢io
做用域链再走一波console
1.全局做用域
- a
- testFun2
testFun2
- a
- test
test
- 无
运行时test时,报告老大,我须要变量a ,test做用域:我没有啊,你去看看testFun2有没有;
testFun2做用域:我有 给你拿去好了;
输出 300
实例
'use strict'; x = 3.14; // 报错(x 未定义)
'use strict' myFunction(); function myFunction(){ y = 3.14; // 报错 (y 未定义) }
// 不容许删除变量或对象 'use strict'; var x = 3.14; delete x ;
// 不容许删除函数 'use strict'; function x(p1,p2){}; delete x ; // 报错
// 不容许变量重名 'use strict'; function x(p1,p1){} // 报错
// 不容许使用八进制 'use strict'; var x = 010; // 报错
// 不容许使用转义字符 'use strict'; var x= \010; // 报错
// 不容许对只读属性赋值 'use strict'; var obj ={}; Object.defineProperty(obj,'x',{value:0,writable:false}); obj.x = 3.14;
// 不容许对一个使用 getter方法读取的属性进行赋值 'use strict'; var obj ={get x(){return 0}}; obj.x=3.14; // 报错
// 变量名 不能使用 'eval'字符串 'use strict'; var eval = 3.14; // 报错
//变量名不能使用 'arguments'字符串 'use strict'; var arguments = 3.14; //报错
// 禁止 this 关键字指向全局对象 function test(){ console.log(this); // undefined }
- implements
- interface
- let
- package
- private
- protected
- public
- static
- yield