Nanyi
JavaScript is an object-based (object-based) language, and everything you encounter is almost always an object. However, it is not a real object-oriented programming (OOP) language because it has no class (class) in its syntax.
So, what should we do if we want to encapsulate the property and method as an object and even generate an instance object from the prototype object?
First, the original mode of the generated object
Suppose we think of a cat as an object that has two properties of "name" and "Color".
var Cat =""
Now we need to generate two instance objects based on the schema of the prototype object.
var // Create an empty object // According to the property assignment of the prototype object = "Yellow"; var cat2 == "Er Mao"
Well, this is the simplest package, encapsulating two attributes in an object. However, there are two shortcomings in this writing, one is that if you generate several instances, it is very troublesome to write, and the other is that there is no way between the example and the prototype, and we can see what the connection is.
Ii. improvement of the original model
We can write a function that solves the problem of code duplication.
function Cat (name,color) { return
Then generate the instance object, which is equivalent to calling the function:
var cat1 = Cat ("Da Mao", "Yellow"); var cat2 = Cat ("Er Mao", "Black");
At this point, the problem comes. Then how to Output "Mao"
Please answer:?
The problem with this approach remains that there is no intrinsic connection between CAT1 and CAT2, and it does not reflect that they are instances of the same prototype object.
Third, the structural function mode
To solve the problem of generating instances from prototype objects, JavaScript provides a constructor (Constructor) pattern.
The so-called "constructor" is actually a normal function, but the this variable is used internally. Using the new operator on a constructor enables you to generate an instance, and the this variable is bound to the instance object.
For example, a cat's prototype object can now be written like this,
function Cat (name,color) { this. name=name; this. color=
We can now build the instance object.
varnew Cat ("Da Mao", "Yellow"); varnew Cat ("Er Mao", "Black"/// fluffy // Yellow
At this point, CAT1 and Cat2 automatically contain a constructor attribute that points to their constructors.
// true //true
JavaScript also provides a instanceof operator that validates the relationship between a prototype object and an instance object.
instanceof // true instanceof//
Four, the problem of the structure function pattern
The constructor method works well, but there is a problem of wasting memory.
See, we now add a constant property "type" for the Cat object, and then add a method eat (Eat mouse). So, the prototype object cat becomes the following:
function Cat (name,color) { this. Name = name; this. color = color; this. Type = "Cat animal"; Thisfunction() {alert ("Eat mouse"
The same approach is used to generate an instance:
varnew Cat ("Da Mao", "Yellow"); varnew Cat ("Er Mao", "Black"// feline //
There seems to be no problem on the surface, but there is a big drawback in actually doing so. That is, for each instance object, the type attribute and the Eat () method are identical, and each time an instance is generated, it must be a duplicate of the content and occupy more memory. This is neither environmentally friendly nor inefficient.
// false
Can the type attribute and the Eat () method be generated only once in memory, and then all instances point to that memory address? The answer is yes.
V. Prototype mode JavaScript specifies that each constructor has a prototype property that points to another object. All properties and methods of this object are inherited by an instance of the constructor. This means that we can define the invariant properties and methods directly on the prototype object.
function Cat (name,color) { this. Name = name; this. color == "Feline"function
Then, build the instance.
varnew Cat ("Da Mao", "Yellow"); varnew Cat ("Er Mao", "Black"// feline // eat mice
At this point the type attribute and the Eat () method for all instances are actually the same memory address, pointing to the prototype object, thus improving the efficiency of the operation.
//
Six, the verification method of prototype mode
To match the prototype property, JavaScript defines some helper methods that help us to use it. ,
6.1 isprototypeof ()
This method is used to determine the relationship between a Proptotype object and an instance.
// true //
6.2 hasOwnProperty ()
Each instance object has a hasOwnProperty () method that is used to determine whether a property is a local property or a property inherited from a prototype object.
// true alert (Cat1.hasownproperty (//
6.3 In operator
The in operator can be used to determine whether an instance contains a property, whether it is a local property or not.
inch // true alert (in// true
The in operator can also be used to traverse all properties of an object.
for (var in
Javascript Object-oriented programming (i): encapsulation