Home > Web Front-end > JS Tutorial > javascript object oriented preliminary object-oriented programming_basic knowledge

javascript object oriented preliminary object-oriented programming_basic knowledge

WBOY
Release: 2016-05-16 19:21:04
Original
1191 people have browsed it

Use new Object() to create objects
There are several ways to create objects in JavaScript, and different methods can be used in different situations. The simplest is to use the new operator, for example:

Copy code The code is as follows:


We defined the person object in this example, and then added its attributes and methods. In this example, the custom method has two attributes.

Use literal notation Literal Notation creates objects
Objects can also be created using text notation, but JavaScript version 1.2 or above is required. Its creation forms are diverse.
Copy code The code is as follows:


Literal tokens can be arrays or any javascript expressions or values.

Creating a custom object using the new operator or literal notation is simple and logical. But its biggest disadvantage is that the result cannot be reused. It is also not easy to initialize and create objects with different versions. For example In the first example above, if person's name is not "Tim Scarfe", then we have to redefine the entire object just to adapt to its slight changes.

Construction and prototype of the object

In the world of OOP, defining objects using previous methods has limitations in many situations. We need a way to create objects whose types can be used multiple times without redefining them. The object can be instantiated every time Assign different values ​​as needed. The standard way to achieve this goal is to use the object constructor function.

An object constructor is just a regular javascript function. It is like a container (defining parameters, calling other function, etc.). The difference between them is that the constructor function is called by the new operator (as you will see in the example below). Based on the object definition in the form of function syntax, we can make it work best Okay.


Let us use a cat in the real world as an example. The cat's name and color are the attributes of the cat. Meow (meow) is one of its methods. The important thing is any difference Cats may all have different names and meows. In order to build an object class that adapts to these characteristics, we will use the object constructor.
Copy code The code is as follows:


Here, the function cat() is an object constructor, and its properties and methods are represented by this in the function body. Definition, the object defined with the object constructor is instantiated with new. Let’s see how we can easily define multiple instances of cat. Each one has its own name. This is the flexibility that the object constructor brings us.
The constructor creates a blueprint of the object. It is not the object itself.
Add methods to the prototype.
In the example we saw above, the object's methods are defined in the constructor. Another One way to achieve this is through prototype.xxx
Prototype is a form of JavaScript inheritance. After we define the object, we can create a method. All instances of the original object will be shared.
Let Let's extend the original cat object. Add a rename method. Use prototype.
Copy the code The code is as follows:


As you can see. We only use the keyword prototype to implement the object The changeName method is added immediately after definition. This method is shared by all instances.
Use prototype to overload javascript objects
Prototype can work on both custom objects and selectively overloaded objects. For example Date() or String The possibilities are endless.
Subclasses and Superclasses
In JAVA and C, there is an external concept of class hierarchy. Each class can have a role.
In Java and C, there is an explicit concept of the class hierarchy. i.e. Every class can have a super class from which it inherits properties and methods. Any class can be extended, or sub-classed so the resulting subclass can inherit its parent's behavior. As we have seen, javascript supports prototype inheritance instead of class based. It's possible for inheritance to happen other ways, however.

The following is an example of inheritance through functions.

The following example Demonstrates how to inherit through function. type="text/javascript">



Prototypes based on inheritance are useful in many JavaScript applications.
(Prototype-based inheritance is very useful in many JavaScript applications.)

Objects as associative arrays
As you know, the (.) operator can be used to store. The [] operator is used to operate on arrays.

How do I loop through properties in an object?
You need to use a for /in loop.
We can traverse the properties of the object through the for in loop.



There are too many errors on the Internet, jb51.net corrected the test

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> function Car(color,door){ this.color = color; this.doors = door; this.arr = new Array("aa","bb"); } Car.prototype.showColor=function(){ alert(this.color); } var car1 = new Car("red",4); var car2 = new Car("blue",4); car1.arr.push("cc"); car1.arr.unshift("dd"); alert(car1.arr.toString()); car1.showColor(); //output:aa,bb,cc car2.showColor(); //output:aa,bb </script>
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