Block-level functionsStrict mode should be prohibited before ES6. Available since ES6, the scope of a function is the block in which it is declared. This should be disabled in non-strict mode. if(true) { function test() { //Block-level function console.log(1); } } test(); Directly modify the prototype of the objectThe browser optimizes the prototype and plans the memory location for the object's method in advance before calling the instance. Therefore, the prototype cannot be modified directly. The following two methods should be prohibited Use Object.setPrototypeOf to modify the prototype function a(){} a.prototype = { a_prop: "a val" }; function b(){} var proto = { b_prop: "b val" }; Object.setPrototypeOf( proto, a.prototype ); b.prototype = proto; var test = new b; console.log(test.a_prop); // a val console.log(test.b_prop); // b val Directly modify the object's __proto__ attribute function a(){} a.prototype = { a_prop: "a val" }; function b(){} var proto = { b_prop: "b val", __proto__: a.prototype //directly modify the __prototype__ attribute of object b}; b.prototype = proto; var test = new b; console.log(test.a_prop); // a val console.log(test.b_prop); // b val withUsage of with: var a = { p1: 1, p2: 2 } with (a) { p1 = 3; } console.log(a.p1); The use of with should be prohibited, for example: function a(arg1, arg2) { with (arg2){ console.log(arg1); // Can't determine whether to output the first parameter or the arg1 property of arg2} } var arg2 = {arg1:1} a("arg1", arg2) calleearguments.callee represents the currently executing function: function a(arg1) { if (arg1 > 1) { return arg1 * arguments.callee(arg1 - 1); } else { return 1; } } console.log(a(3)); // 6 When a function must call itself, use arguments.callee() instead and call the function directly by its name. function a(arg1) { if (arg1 > 1) { return arg1 * a(arg1 - 1); // Call directly by function name} else { return 1; } } console.log(a(3)); // 6 callercaller represents the caller of the function and should not be used. This feature is not standard. function a() { console.log(a.caller); // function b() { a(); } } function b() { a(); } b(); evaleval() can execute the passed string parameter as JavaScript code. eval("var a = 1, b = 2; console.log(a+b)"); // 3 Use of eval is prohibited. Eval is slower than normal JavaScript execution because browsers optimize JavaScript. The eval method is also unsafe because it executes code with the same permissions as the caller, and its scope is exposed when eval() is called. Function should be used instead: var a = new Function("a", "b", "console.log(a+b)") a(1,2); // 3 The above is the detailed content of the writing methods that should be prohibited in native JS. For more information about the writing methods that should be prohibited in native JS, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem
>>: Nginx configuration cross-domain request Access-Control-Allow-Origin * detailed explanation
1. Installation Package MYSQL service download ad...
Table of contents What is a trigger Create a trig...
Firefox, Opera and other browsers do not support W...
1. Software Download MySQL download and installat...
Downloaded the unzipped version of mysql-5.6.37-w...
This article example shares the specific code for...
1. Triangle Border settings Code: width: 300px; h...
Adding indexes can improve query efficiency. Addi...
Table of contents tool Install the plugin Add a ....
A mysql-like php switch case statement. select xx...
Table of contents background 1) Enable the keepch...
Introducing vue and vue-router <script src=&qu...
MySQL 8 brings a brand new experience, such as su...
1. Error error connecting to master 'x@xxxx:x...
The Truncate table statement is used to delete/tr...