Home > Web Front-end > JS Tutorial > Object-oriented JavaScript (1)_javascript skills

Object-oriented JavaScript (1)_javascript skills

WBOY
Release: 2016-05-16 19:24:33
Original
1067 people have browsed it

one. The traditional approach based on prototype
To be precise, the "class" of Javascript is not a real class in a strict sense. There is such a relationship from the declared object to the class (to put it this way):
Instance.__proto__=InstanceClass.prototype (ie not supported)
The above description: the attribute of the instance (__proto__) is equal to the prototpye of the class. We can see that the "__proto__" of the instance happens to be connected to the "prototype" of the class ", through the prototype chain (prototype) to find its method and extend it, we will find that its method (Method) and its properties (Property) are both Public (of course you can also use Private, that is, without this), and prototype is actually a Object, used to record methods as a collection
Suppose we declare a class A, it can be like this
function A(){
this.className="ClassA"; //Add the Public attribute, to be precise The class name should be Private
}
//Add a method
A.prototype.getClassName=function(){
return this.className; //this refers to A, not prototype
}
//Declare an object
var AObj=new A();
alert(AObj.__proto__==A.prototype); //Invalid under ie, true under ff, indicating the above The verification is correct
// Let’s take a look at the object attributes
/*
Since this is used, className is Public, which can be modified or read at will
If you want to protect it, just To use Private, you can remove its this prefix as a protection
When reading, use a public method, that is, the method under prototype:
A.prototype.getClassName=function(){
return className ; At this point, it is indeed not as good as the second method, which will be explained later
*/
About "prototype"
Since Javascrīpt has not used "inheritance" within the system (very Most likely), so the prototype under class gets all
top-level Object. When inheriting later, we can see such a formula:
child.prototype=new parent();
The word inheritance in quotation marks means that even if there is inheritance, it is not inheritance in the true sense, and it is only obtained through prototype.
Use an instance to extend a method to a class. We have to mention the __proto__ attribute here



Copy the code The code is as follows:

Under ff I can see the instance name.__proto__=class.prototype. Now extend a method through the instance. This is not recommended in practical applications. The following example:
s.__proto__.hjk=function(){
return "hjk";
}
alert(s.hjk());
for(var t in abc.prototype){
alert("prototype points to:" t "=" abc .prototype[t]);
}
You should understand now.
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