Home > Article > Web Front-end > What are the disadvantages of javascript constructor methods
The disadvantage of the JavaScript constructor is that every time an object is instantiated, the properties and methods will be copied; if there are the same methods between instances, although the properties and methods defined in the constructor are accessible, However, each instance is copied, which will cause a waste of system resources.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
The advantage is that the object can be identified through instanceof. The disadvantage is that every time an object is instantiated, the properties and methods will be copied again
function CreateObj(uName) { this.userName = uName; this.showUserName = function () { return this.userName; } } var obj1 = new CreateObj('ghostwu'); var obj2 = new CreateObj('卫庄'); console.log( obj1.showUserName === obj2.showUserName ); //false
From the above execution results, we can see that obj1.showUserName and obj.showUserName are not the same [in js, the reference type compares the address, the function is a reference type], but there are two different The memory address of
, because the attributes of each object are different, there is no problem with this, but the methods execute the same code, so there is no need to copy, there are multiple copies, which wastes memory. This is Disadvantages
Extended knowledge
Constructor: If a function is used to initialize (using the new operator) a newly created object, we call it For the constructor (constructor)
function Person(){ this.name = "zqq"; this.age = 28; } var p = new Person();
When the constructor is called with new (execution var p = new Person()), the following will happen inside the function:
1. Create an empty object
var p = {};
2.This variable points to the object p
Person.call(p)
3.p inherits the prototype of the constructor Person()
p.__proto__ = Person.prototype
4. Execute the constructor Person() Code
The difference between constructor and ordinary function:
1. The constructor is called using the new keyword; ordinary functions are not called with the new keyword;
var p = new Person(); var p = Person();
2. The this keyword can be used inside the constructor; it is not recommended to use this inside a normal function, because this points to the window global object at this time, so some global variables or functions will be added to the window unintentionally
Inside the constructor, this points to the constructed new object
Inside the ordinary function, this points to the window global object
3. The constructor does not return a return value by default; Ordinary functions generally have a return value
The constructor will return this by default, which is the new instance object
If an ordinary function does not have a return value, it will return undefined
If If return is used, the return value will vary depending on the type of return value
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of What are the disadvantages of javascript constructor methods. For more information, please follow other related articles on the PHP Chinese website!