Writing methods that should be prohibited in native JS

Writing methods that should be prohibited in native JS

Block-level functions

Strict 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 object

The 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

with

Usage 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)

callee

arguments.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

caller

caller 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();

eval

eval() 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 write memory-efficient applications with Node.js
  • A brief discussion on the execution efficiency of js regular literals // and new RegExp
  • JavaScript checks the code running efficiency console.time() and console.timeEnd() usage
  • How to improve JavaScript loading and execution efficiency
  • Several ways to write for loops in JavaScript and their efficiency summary
  • Efficiency test of several methods for deduplication of JavaScript arrays
  • How to efficiently remove duplicate items in js array
  • In-depth exploration of the efficiency issues and related optimizations of for loops in JavaScript
  • Three methods of determining whether JavaScript is an array and their efficiency comparison

<<:  How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem

>>:  Nginx configuration cross-domain request Access-Control-Allow-Origin * detailed explanation

Recommend

Detailed explanation of the use of MySQL sql_mode

Table of contents Preface sql_mode explained The ...

Detailed explanation of filters and directives in Vue

Table of contents vue custom directive Global Dir...

Let's talk about the characteristics and isolation levels of MySQL transactions

The Internet is already saturated with articles o...

Detailed explanation of how to dynamically set the browser title in Vue

Table of contents nonsense text The first router/...

How to View All Running Processes in Linux

You can use the ps command. It can display releva...

js to realize a simple disc clock

This article shares the specific code of js to im...

Win2008 R2 mysql 5.5 zip format mysql installation and configuration

Win2008 R2 zip format mysql installation and conf...

What is Makefile in Linux? How does it work?

Run and compile your programs more efficiently wi...

Detailed explanation of Vue life cycle

Table of contents Why understand the life cycle W...

Detailed steps for installing and configuring MySQL 8.0 on CentOS

Preface Here are the steps to install and configu...

Several ways to pass data from parent components to child components in Vue

I have been studying the source code of Vue recen...

The most commonly used HTML tags to create web pages

1. Optimization of commonly used HTML tags HTML s...

Tutorial on installing GreasyFork js script on mobile phone

Table of contents Preface 1. Iceraven Browser (Fi...

CSS sets Overflow to hide the scroll bar while allowing scrolling

CSS sets Overflow to hide the scroll bar while al...