The difference between the normal functions and constructor in JS 2021-06-19

tags: JS senior learning  js

First, the built-in class in JS

Common built-in class in JS

  1. Every data type has its own built-in class
    Number、String、Boolean、Symbol、BigInt
    Object、Function、Array、Date、RegExp、Error(ReferenceError/TypeError/RangeError/SyntaxError)…
  2. Each element object also has its own built-in class
  3. The rest of the built-in class
    XMLHttpRequest、Promsie、Proxy…

Custom class "class-> constructor"
ES5: Create a function, perform a function based on "New", this function is called the constructor "class", the result is an instance of the class
ES6: Create a custom class based on "Class"

The difference between ordinary function execution and new execution functions

JS code runtime:

  1. Create an EC (G) in the store - Global execution context
  2. Create VO (G) / GO in EC (G), store global variables, and improve variables
  3. Open an address storage function plot in the heap memory, the storage function scope in the heap, the code string and function in the function (Name, Length)
  4. Code execution

Ordinary function execution:

  • Create an EC (Fn) - function execution context in the stack memory
  • Create AO (FN) in EC (FN), store function private variables
  • Function execution: Initialization scope chain, initial THIS, initial arguments, meticulum assignment, variable lifting, code execution

NEW execution function: (execute the function as constructor)

  • Create an EC (Fn) - function execution context in the stack memory
  • Create AO (FN) in EC (FN), store function private variables
  • An instance object that creates an empty object (FN) by default
  • Function execution: Initialization scope chain
  • Initial THIS, let this default to the instance objects previously created
  • Initial arguments, meticulum assignment, variable lifting
  • Code Execution: In the post-function code execution, this.xxx = xxx appears is a private property and method set to instance objects.
  • If the return value is not set in the function (or the return value is the value of the original type), the default returns "instance object", only the value of the object type is the value of the object type, is mainly

example:

function Fn(x, y) {
    let sum = 10;
    this.total = x + y;
    this.say = function () {
        console.log(`I calculate and is:${this.total}`);
    };
}
// let res = fn (10, 20); // Normal function execution => FN function RES function return value
// console.log(res); //undefined
// console.log (window.total, window.say, window.sum); // 30 functions undefined

let f1 = new Fn(10, 20); // Constructor execution => FN constructor (class) F1 class instance
console.log(f1); // Example Object Total & Say

Intelligent Recommendation

What is the difference between arrow functions and normal functions?

What is the difference between arrow functions and normal functions? Ordinary functions this: This is always representing its direct caller. By default, the direct caller is not found, this refers to ...

2018-06-19: The difference between $ and $! in Velocity

The difference between $ and $! The $!{obj} ! symbol indicates that the value is displayed if a value can be obtained. If the value is not obtained or the value is null, the empty string is output &qu...

What is the difference between arrow functions and normal functions (function)? Constructor can generate an instance using the New, then the arrow function can? why?

Difference of arrow function and normal functions 1. Arrow function syntax is more simple than normal functions (each function in ES6 can use shape to participate in default values ​​and remaining ope...

In JS, the difference between function and constructor

There are two concepts of constructor and function in JavaScript. This may cause some confusion for beginners, it is difficult that there are two functions. However, the composition of these two funct...

The difference between await followed by promise() and normal functions

Async ES7 keyword, which means asynchronous, can be used alone is used in front of the function, declare that this is an asynchronous function, and then execute it after the synchronization code is ex...

More Recommendation

Difference between function template and normal functions

"If your light source is in the heart, you will never lose" Rimmy The last time I have mentioned this problem in the "Overload" of Function Template, but I didn't care. Now let's t...

Learning: Constructor and normal functions

Constructor (forNew instance object) And normal function differences: The constructor is the same, but the general constructor is generally constructed.First letter is uppercase; Constructor call mode...

Constructor and normal functions

The constructor is also a normal function, the creation method is the same as the normal function, but the constructor is used to the first letters. The difference between constructor and ordinary fun...

The difference between constructor execution and ordinary functions

The difference between constructor execution and ordinary functions Ordinary function execution Form a new private context Initialization Scheme Chain <own context, superior context> Initialize ...

The difference between constructor and ordinary functions in JavaScript

Let's take a look at the function and constructor's small example Definition of function Definition of constructor The two have the same output, all of which are 3 Function of function contains functi...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top