1、在未使用严格模式下,下面的代码是可以执行的
const obj = {
name: 'peter'
}
function foo(){
with(obj) {
console.log(name)
}
}
foo()//peter2、而当我们开启了严格模式后,将会直接禁用with语法
1、未使用严格模式下的function中的this指向
function foo(){
console.log(this)//指向window
}2、而当我们开启严格模式后
function foo(){
console.log(this)//undefined
}1、未使用严格模式时,以下语法是被允许,变量会被定义为全局变量
function foo(){
name = "peter"
}
console.log(name)//peter2、这个变量命名在严格模式下是不被运行,执行时将会报错
1、未使用严格模式,我们可以给一个函数命名重复的参数名
function foo(a,b,a) {
console.log(a,b,a)
}
foo(1,2,3)//1,2,32、而当开启了严格模式,这种书写方式将会报错:SyntaxError: Duplicate parameter name not allowed in this context
1、之前的八进制书写格式
const num = 0123
console.log(num)//832、而在严格模式下,这种书写是不被允许:SyntaxError: Octal literals are not allowed in strict mode.
但是我们可以改成以下的格式
"use strict"
const num = 0o123
console.log(num)//831、未使用严格模式时,下面对obj对象中的name进行修改虽然是失败的,但是不会抛出错误
const obj = {
name: 'peter'
}
Object.defineProperty(obj, 'name', {writable: false})
obj.name = 'lisa'2、然而当我们开启了严格模式的情况下,对name的修改则会抛出错误:TypeError: Cannot assign to read only property 'name' of object '#<Object>'
"use strict"
const obj = {
name: 'peter'
}
Object.defineProperty(obj, 'name', {writable: false})
obj.name = 'lisa'
1、未使用严格模式
const jsStr = 'var message = "hello world"; console.log(message)'
eval(jsStr)//hello world
console.log(message)//hello world2、使用严格模式后:ReferenceError: message is not defined
"use strict"
const jsStr = 'var message = "hello world"; console.log(message)'
eval(jsStr)
console.log(message)TypeError: Cannot delete property 'name' of #<Object>
"use strict"
const obj = {
name: 'peter'
}
Object.defineProperty(obj, 'name', {writable: false,configurable: false})
delete obj.name