Home > Web Front-end > JS Tutorial > JavaScript constructor analysis

JavaScript constructor analysis

高洛峰
Release: 2016-11-26 14:42:55
Original
1203 people have browsed it

Principle of constructor
JavaScript language is an object-oriented language, but there is no concept of class in JavaScript. So JavaScript uses constructors to simulate the effects of classes, that is, we create objects through functions. This also proves that functions play a very important role in JavaScript. For example: b Var Book = Function (name, Price) {

this.Name = name;

this.price = price;
}}

var java = New Book ('Master Java', 82);

When the new keyword is used to call the constructor, the execution context changes from the global variable object (window) to an empty context, which represents the newly generated instance. Therefore, the this keyword points to the currently created instance


By default, if nothing is returned in your constructor, this----the current context, which is the object you are currently creating, will be returned. Otherwise, any non-primitive type value is returned. Ordinary functions will return undefined if there is no obvious return value.

Simulate constructor to create objects

In fact, the process of using new to create an object is not mysterious. We can use code to simulate how to create an object:


var Book = function(name) {

this.name = name;

};

               //Normal usage
        var java = new Book ('Master Java'); python.__proto__ = Book;

   Book.call(python, 'Master Python’);

  
Call the constructor as an ordinary function
In fact, the constructor is an ordinary function and can be called as an ordinary function, such as var result = Book();, of course the result is undefined. The difference lies in the pointer of this in the function. Let's re-modify the above constructor,


var Book = function(name, price) {
this.name = name;
this.price = price;
if (this == window) {
return 'name = ' + name + ", price =" + price;

}}}


The result of executing varress = book ("java", 100) is "name = java, price = 100", and var java = new The result of Book("Java", 100) is that a new object is created. Many of the constructors that come with JavaScript serve as both ordinary functions and constructors. For example, when String and Number are used as constructors, var a = new Number(11); var b = new Number(11.222); var c = new String("hello");
                When String and Number are used as ordinary functions, they can be used for type conversion, that is, the Number function can convert other types to the number type, and the String function can convert other types to strings,


    var a = Number('11'); //11
  var b = Number('hello'); //NaN
  var c = String(11); //'11'
  var d = String(null); //' null'
     

Summary

When a function is to be used as a constructor, the first letter of the function name is usually capitalized to indicate that it is a constructor. At the same time, new must be used to call the constructor. In addition, try not to use the constructor as an ordinary function, because it is easy to generate global variables, and if the return value is not handled well, it will easily affect the function of the constructor itself.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template