当前位置: 首页 > 网络应用技术

How to use strict mode?

时间:2023-03-09 00:30:29 网络应用技术

  $ color {live} {ecmascript 5} $ starts to add strict mode. In many aspects, the semantics of JavaScript is changed, making JavaScript run under stricter conditions. The browser can analyze the code easier. Now it has been viewed by most of them.Increase (including IE10).

  It is to form a semantics different from normal code, not just a subset of JavaScript. The main purpose is:

  Strict mode can be applied to the entire script or individual functions.In the script string that is transmitted in the EVAL, Function, Inner Union event processing attributes, WindowTimers.Settimeout () methods, its behavior is similar to a single script that enables strict models, and they work as expected.It is no effect in the separate blockbuster arc {}.

  To open the strict mode for the entire script file, you need to put a specific statement "use strict" before all statements; (or 'use strict';).

  There are traps in this grammar. The scripts that are combined in strict mode may be no problem, but there may be problems in the combined strict mode and non -strict mode.Therefore, you cannot merge the conflict code blindly. It is recommended to open the strict mode according to the functions (at least in the transition period of learning).

  To open a strict mode to a function, "use strict"; (or 'use strict';) is placed in front of all statements of the function body.

  The strict mode also changes the grammar and runtime behavior. Changing is usually divided into these categories: the problem is directly transformed into errors (such as grammar errors or runtime errors), simplifying how to calculate how to calculate the specific variables of the given name, simplify EVALAnd arguments.

  JavaScript旨在使新移民更容易开发,因此有时对错误的语义进行了错误的操作。有时这可以解决当前的问题,但有时将来会留下更大的问题。严格的模型将这些错误视为错误以查找并立即纠正它们。

  //到非写入属性分配让obj1 = {};object.defineproperty(obj1,“ x”,{value:42,write:false});obj1.x = 9;//丢弃TypeError错误

  //分配仅读取属性令obj2 = {get x(){return 17;}}};obj2.x = 5;//丢弃TypeError错误错误

  //要将新属性提供给不可扩展的对象,分配让固定= {};object.prevenextensions(固定);field.newprop =“ ohai”;//投掷TypeError错误错误

  false.true =“”; // typeError(14).sailing =“ home”; // typeError“ with .you =“ far away”; // typeError});

  替换的一种简单方法是将目标对象提供给短名称变量,然后访问该变量上的相应属性。

  令X;删除x;//!

  评估(“让y; delete y;”);//!

  在浏览器环境中,JavaScript可以获取用户隐私信息。因此,必须在跑步之前部分转换此类JavaScript以审查对禁止功能的访问。JavaScript的灵活性使得不需要运行时检查就无法执行此操作。要打开严格的模式并以特定的方式致电,它将大大减少必要的内容在执行过程中进行检查,从而避免绩效损失。

  受限制的 ();

  “严格的模型”反映了JavaScript的更合理,更安全,更严格的开发方向,有助于理解JavaScript更详细,深入了解,并使您成为更好的程序员。