为什么讨论es5.0

计算机的执行环境是es3.0和es5.0新增的语法部分,es5.0严格模式,会在es3.0和es5.0发生冲突的时候使用es5.0的语法

怎么使用es5.0

全局模式:

在第一行写上字符串“use strict”

局部模式:

某个方法里面写上“use strict”

es5增加的语法

不允许使用with

with改变作用域链,达到代码简化
语法格式:
with(对象),那么作用域链的最顶端是对象,如果找不到对象的属性方法的时候会按照正常的作用域去执行程序
缺点:降低了程序的性能,每次都会改作用域连太耗费性能

arguments.caller 和callee不能使用

变量赋值前必须声明(重要)

局部的this必须被赋值,this必须被赋值(预编译this不再指向window)

例如:
非严格模式

  1. console.log(this)
  2. function test(){
  3. console.log(this)
  4. }
  5. test()
  6. function Test(){
  7. console.log(this)
  8. }
  9. new Test()
  10. function Test(){
  11. console.log(this)
  12. }
  13. Test.call(123)

执行结果:
image.png

  1. "use strict"
  2. console.log(this)
  3. function test(){
  4. console.log(this)
  5. }
  6. test()
  7. function Test(){
  8. console.log(this)
  9. }
  10. new Test()
  11. function Test(){
  12. console.log(this)
  13. }
  14. Test.call(123)

严格模式结果:
image.png

es3重复的参数和属性不报错,但是在es5报错

eval()