錯誤類型:SyntaxError: Delete of an unqualified identifier in strict mode
使用前:
// variable (沒有作用)var x =3.14;console.log( delete x ); // falseconsole.log(x); // 3.14// function via declararion (沒有作用)functionf1(){ console.log('Hi 1'); }console.log( delete f1 ); // falseconsole.log(f1); // ƒ f1(){ console.log('Hi 1'); }// function via expression (沒有作用)varf2=function() { console.log('Hi 2'); }console.log( delete f2 ); // falseconsole.log(f2); // ƒ () { console.log('Hi 2'); }// array elementvar fruits = ["Banana","Orange","Apple","Mango"];console.log( delete fruits[2] ); // trueconsole.log(fruits); // ["Banana", "Orange", empty, "Mango"]// object propertyvar person = {name:'John', age:18};console.log( deleteperson.name ); // trueconsole.log(person); // {age: 18}
使用後:
"use strict";// variablevar x =3.14;console.log( delete x ); // SyntaxError: Delete of an unqualified identifier in strict mode.// function via declararionfunctionf1(){ console.log('Hi 1'); }console.log( delete f1 ); // SyntaxError: Delete of an unqualified identifier in strict mode.// function via expressionvarf2=function() { console.log('Hi 2'); }console.log( delete f2 ); // SyntaxError: Delete of an unqualified identifier in strict mode.// array elementvar fruits = ["Banana","Orange","Apple","Mango"];console.log( delete fruits[2] ); // trueconsole.log(fruits); // ["Banana", "Orange", empty, "Mango"]// object propertyvar person = {name:'John', age:18};console.log( deleteperson.name ); // trueconsole.log(person); // {age: 18}
3. 函數的參數名稱不能重複
一般模式下,函數的參數名稱如果不慎重複,不會有提醒。
錯誤類型:SyntaxError: Duplicate parameter name not allowed in this context
錯誤類型:TypeError: Cannot set property age of #<Object> which has only a getter
使用前:
var person = {getage() {return18} };console.log(person); // {}console.log(person.age); // 18person.age =70;console.log(person.age); // 18
使用後:
"use strict";var person = {getage() {return18} };console.log(person); // {}console.log(person.age); // 18person.age =70; // TypeError: Cannot set property age of #<Object> which has only a getterconsole.log(person.age);