javascript constructor method to define objects_Basic knowledge
- WBOYOriginal
- 2016-05-16 16:22:381538browse
Javascript is a dynamic language that can add attributes to objects at runtime and delete attributes from objects
Attribute name: method name is also acceptable. Because the function itself is an object
javascript array sorting
Several ways to define objects in JavaScript (there is no concept of classes in JavaScript, only objects)
The first way: expand its properties and methods based on existing objects
This method has limitations, because javascript does not have the concept of a class like java. Write a class, and then new can get an object with these properties and methods.
If you want to own object2 at this time, you can only write another copy of the code above, which is not good.
Second method: Factory method
Similar to the static factory method in java.
Copy code The code is as follows:
The above method of creating objects has disadvantages (each object has a get method, thus wasting memory), the improved factory method (all objects share a get method):
Third way: constructor method to define objects
Fourth method: Create objects using prototype method
Prototype is an attribute in the object object, and all person objects can also have the prototype attribute.
You can add some attributes and methods to the prototype of the object.
Disadvantages of simply using the prototype method to create objects: ①Unable to pass parameters, you can only change its value after the object is created
② may cause program errors
Statement: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