Js functions

First, figure out what a function is. According to the Rhino book, a function is simply a block of code that executes repeatedly. A function is a piece of JavaScript code that is defined only once, but can be executed or called any number of times.

Function definition:

Function name () {}; This way, the function declaration will be promoted to the beginning of the scope of the function, and you can call the function for your use wherever you use the function declaration in the smallest scope of the function.

Let fun = function(){}; Functions defined this way can only be called through fun () in that scope after the assignment code executes, otherwise fun === undefined due to the variable declaration enhancement.

Var fun1 = new Function (arg1, arg2,arg3… , argN, body); All arguments to the Function constructor are strings. All arguments except the last are taken as arguments to the generator function. You can have no arguments here. The last argument represents the body of the function to be created.

Conclusion: 1. The first and second methods of Function definition are actually syntactic sugar of the third new Function. When we define a Function, we will create a Function through new Function, but the first two methods are encapsulated for us, we can’t see them. Any Function in js is an instance of Function. 2. Ecmascript-defined functions are actually fully functional objects.

A function that instantiates an object by the name of a new function is called a constructor. Any function can exist as a constructor. The main function of the constructor is to initialize the object, which is used with new. New is creating an object from scratch, and the constructor is adding properties and methods to the initialized object. Constructors are defined with a capital letter (specification).

New allocates memory and creates objects. When new is called, new Object() is implicitly executed in the background to create objects. Therefore, strings and numbers created by new are reference types, not value types.

1, common constructors:

1. var arr = []; Var arr = new Array(); Grammar sugar.

Var obj = {} var obj = new Object(); The syntactic sugar

3. Var date = new date ();

4….

2. What happens when the constructor is executed:

Let f = new Foo(); Function Foo(name,age,sex){this.name = name; this.age = age; this.sex = sex; } foo.prototype. belief = function(){console.log(' console.log '){console.log(' console.log '){console.log(' console.log '); '); } let f = new Foo ('zh',18,' male '); a . let f = {}; // A new object inherited from foo. prototype is created. b. f.__proto__ = Foo.prototype; // f inherits the prototype from Foo. B Foo. Call (f, 'useful', 18, 'male'); // Foo (name,age,sex); // Foo (name,age,sex); f.age = 18; F. Ex = 'male'; C. Instantiate the object. At this point f = {name: 'useful', age: 18, sex: 'male'} d. f.b elief (); Print 'quantitative change is the necessary preparation for qualitative change, qualitative change is the inevitable result of quantitative change accumulated to a certain degree!Copy the code

Handwritten new function

Function newTest (constructFunction){let obj = {}; Obj. __proto__ = constructFunction. Prototype; Return the function () {constructFunction. Apply (obj, the arguments). Return obj. }}Copy the code

Note: When a constructor returns an object, the new object is the constructor’s return value, not the object generated during the new process. This is true only if the constructor returns an object, but if it is not, it still returns the object formed in the new process (the new constructor returns an object value after it anyway).

ES6 class class constructor

Class is the syntactic sugar of a constructor, that is, class is essentially a constructor. The extends extends of a class is essentially an inheritance of a constructor’s prototype chain.

Such as:

The class method

Constructor (name,age){// Constructor is a constructor that takes the parameter this.name = name; //this represents the instance object this.age = age; Function return this.name + this.age}} var obj = new Person(' old Person ', 18); console.log(obj.say());Copy the code

The way you write the constructor

Function Person(name,age){if(! (this instanceof Person)){throw new Error(" please use new Person") to prevent users from accidentally calling Person as a normal function; } this.name = name; this.age = age; } Person. Prototype. Say = function(){return this.name + this.age} var object = new Person(' old ', 18); // The new operator console.log(obj.say()) must be used to create objects using constructors;Copy the code

Conclusion: Classes defined by class and classes defined by constructors are essentially the same. And when JS is executed, the first transfer is executed as the second. So ES6 classes are essentially constructors.