strict mode - garevna/js-course GitHub Wiki
"use strict"Это директива для интерпретатора
Директива 'use strict' распознается только в начале скрипта или функции
function sample () {
'use strict'
...
}Директива 'use strict' переводит выполнение скрипта в строгий режим ( strict mode )
'use strict'
x = 8⛔️ Uncaught ReferenceError: x is not defined
function sum ( x, y ) {
return x + y
}
delete sum // false'use strict'
function sum ( x, y ) {
return x + y
}
delete sum⛔️ Uncaught SyntaxError:
Delete of an unqualified identifier in strict mode.
var x = 010 // 8'use strict'
var x = 010⛔️ Uncaught SyntaxError:
Octal literals are not allowed in strict mode.
var x = "\010" // ""'use strict'
var x = "\010"⛔️ Uncaught SyntaxError:
Octal escape sequences are not allowed in strict mode.
var sample = Object.defineProperty(
{},
"x",
{ value:0, writable:false }
)
sample.x = 5 // 0'use strict'
var sample = Object.defineProperty(
{},
"x",
{ value:0, writable:false }
)
sample.x = 5⛔️ Uncaught TypeError:
Cannot assign to read only property 'x' of object '#<Object>'
var obj = {
get x() {
return 0
}
}
obj.x = 5 // 0'use strict'
var obj = {
get x() {
return 0
}
}
obj.x = 5⛔️ Uncaught TypeError:
Cannot set property x of #<Object> which has only a getter
delete Object.prototype // false'use strict'
delete Object.prototype⛔️ Uncaught TypeError:
Cannot delete property 'prototype' of function Object() { [native code] }
var eval = 7 // 7'use strict'
var eval = 7⛔️ Uncaught SyntaxError:
Unexpected eval or arguments in strict mode
var arguments = 7 // 7'use strict'
var arguments = 7⛔️ Uncaught SyntaxError:
Unexpected eval or arguments in strict mode
function test () {
console.log ( arguments.callee )
}
test ()ƒ test () {
console.log ( arguments.callee )
}
'use strict'
function test () {
console.log ( arguments.callee )
}
test ()⛔️ Uncaught TypeError:
'caller', 'callee', and 'arguments' properties
may not be accessed on strict mode functions
or the arguments objects for calls to them
function test () {
( function () {
console.log ( arguments.callee.caller )
})()
}
test ()ƒ test () {
( function () {
console.log ( arguments.callee.caller )
})()
}
'use strict'
function test () {
( function () {
console.log ( arguments.callee.caller )
})()
}
test ()⛔️ Uncaught TypeError:
'caller', 'callee', and 'arguments' properties
may not be accessed on strict mode functions
or the arguments objects for calls to them
var x, y
with ( String ) {
x = fromCharCode ( 89, 75 )
}
console.log ( x ) // "YK"
with ( Math ) {
y = round ( x = random () * 1000 )
}
console.log ( y ) // 256'use strict'
var x, y
with ( String ) {
x = fromCharCode ( 89, 75 )
}
console.log ( x ) // "YK"
with ( Math ) {
y = round ( x = random () * 1000 )
}
console.log ( y ) // 256⛔️ Uncaught SyntaxError:
Strict mode code may not include a with statement
eval ( "var gamma = 2" )
console.log ( gamma )'use strict'
eval ( "var gamma = 2" )
console.log ( gamma )⛔️ Uncaught ReferenceError:
gamma is not defined
🔴 implements
🔴 interface
🔴 let
🔴 package
🔴 private
🔴 protected
🔴 public
🔴 static
🔴 yield
⛔️ Uncaught SyntaxError:
Unexpected strict mode reserved word