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
Samba Overview Samba is a free software that impl...
How to create a service and auto-start it in Ubun...
This note is an installation tutorial. It has no ...
Because the server's database hard disk space...
Table of contents 1. Troubleshooting and locating...
In fact, we wonder every day when IE6 will really...
MySQL implements sequence function 1. Create a se...
Application example website http://www.uhuigou.net...
Today I was dealing with the issue of migrating a...
mysql id starts from 1 and increases automaticall...
When I first started setting up an ftp server on ...
Preface "High Performance MySQL" mentio...
Table of contents 1. Background 2. Understanding ...
1. The ul tag has a padding value by default in M...
Table of contents 1. Basic Concepts 1.1 Two kinds...