如下是嚴格模式中須要注意的用法,這裏須要強調的是:ES6 的 class 和 模塊內都是默認的嚴格模式。其實,js 開發也會逐步走向嚴格模式,這應該是個趨勢。瀏覽器
(function(){ //非嚴格模式 var a = {name: "Bob"}; with(a){ name = "Lily"; } console.log(a.name); //Lily })(); (function(){ "use strict"; //嚴格模式 var a = {name: "Bob"}, b = {}; with(a, b){ //SyntaxError: Strict mode code may not include a with statement name = "Lily"; } console.log(a.name); })();
(function(){ //非嚴格模式 a = 10; console.log(a); //10 })(); (function(){ "use strict"; //嚴格模式 b = 10; //ReferenceError: b is not defined console.log(b); })();
(function(){ //非嚴格模式 console.log(this); //window })(); (function(){ "use strict"; //嚴格模式 console.log(this); //undefined })();
(function(){ //非嚴格模式 var o = { name: "Lily" }; Object.freeze(o); o.name = "Bob"; o.age = 20; console.log(o); //Object {name: "Bob"} })(); (function(){ "use strict"; //嚴格模式 var o = { name: "Lily" }; Object.freeze(o); o.name = "Bob"; //TypeError: Cannot assign to read only property 'name' of object '#<Object>' o.age = 20; //TypeError: Can't add property age, object is not extensible console.log(o); })();
(function(){ //非嚴格模式 var str1 = "var name='Lily';"; var str2 = "function fun1(){console.log('hello');}"; eval(str1); //這個name定義在了全局,而不是函數內 eval(str2); console.log(name); //Lily fun1(); //hello })(); (function(){ "use strict"; //嚴格模式 var str1 = "var alias='Lily';"; var str2 = "function fun2(){console.log('hello');}"; eval(str1); eval(str2); eval("name = 'Bob'"); //修改全局變量name console.log(name); //Bob console.log(alias); //ReferenceError: alias is not defined fun2(); //ReferenceError: fun is not defined })();
(function(){ //非嚴格模式 var name = "Bob"; test(name); function test(alias){ alias = "Lily"; console.log(alias); //Lily console.log(arguments[0]); //Lily } })(); (function(){ "use strict"; //嚴格模式 var name = "Bob"; test(name); function test(alias){ alias = "Lily"; console.log(alias); //Lily console.log(arguments[0]); //Bob } })();
(function(){ //非嚴格模式 var a = 10; var fun = function(){console.log("fun called");}; var o = Object.defineProperty({}, "name", { value: "Bob" }); //默認即不可配置 delete a; //false console.log(a); //10 delete fun; //false fun(); //fun called delete o.name; //false console.log(o.name); //Bob //刪除一個不存在的變量 delete no; //false })(); (function(){ "use strict"; //嚴格模式 var a = 10; var fun = function(){console.log("fun called");}; var o = Object.defineProperty({}, "name", { value: "Bob" }); //默認即不可配置 //delete a; //SyntaxError: Delete of an unqualified identifier in strict mode. console.log(a); delete fun; //SyntaxError: Delete of an unqualified identifier in strict mode. fun(); delete o.name; //SyntaxError: Delete of an unqualified identifier in strict mode. console.log(o.name); //刪除一個不存在的變量 delete no; //SyntaxError: Delete of an unqualified identifier in strict mode. })();
(function(){ //非嚴格模式 eval = 10; eval("console.log('hello');"); //TypeError: eval is not a function (function(){ arguments = 20; console.log(arguments); //20 }()); })(); (function(){ "use strict"; //嚴格模式 eval = 10; //SyntaxError: Unexpected eval or arguments in strict mode eval("console.log('hello');"); (function(){ arguments =20; //SyntaxError: Unexpected eval or arguments in strict mode console.log(arguments); }()); })();
(function(){ //非嚴格模式 console.log(070); //56 (因瀏覽器而異) })(); (function(){ "use strict"; //嚴格模式 console.log(070); //SyntaxError: Octal literals are not allowed in strict mode. })();
(function(){ //非嚴格模式 var one = 1; var two = 2; fun(one, two); //2 function fun(a,a){ console.log(a); } })(); (function(){ "use strict"; //嚴格模式 var one = 1; var two = 2; fun(one, two); function fun(a,a){ //SyntaxError: Duplicate parameter name not allowed in this context console.log(a); } })();
(function(){ //非嚴格模式 A(); function A(){ B(); } function B(){ console.log(B.caller); //function A(){ B(); } console.log(arguments.callee); //function B(){ console.log(B.caller); console.log(arguments.callee); } } })(); (function(){ "use strict"; //嚴格模式 A(); function A(){ B(); } function B(){ console.log(B.caller); //TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. console.log(arguments.callee); //TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them }