Home > Web Front-end > JS Tutorial > JavaScript prototype learning summary_javascript skills

JavaScript prototype learning summary_javascript skills

WBOY
Release: 2016-05-16 18:17:32
Original
926 people have browsed it

Each object has an implicit property that points to the prototype of its parent object (the function that constructs the object) (here called the parent prototype or implicit prototype), and inherits its properties and methods from it [ In addition to the parent prototype reference, the function object also has an explicit prototype reference]. Under normal circumstances, the parent prototype of the object is not accessible, while the explicit prototype of the function object can be accessed through FunctionName.prototype [In FireFox you can access the object's parent prototype through the object's __proto__ attribute]

This prototype attribute itself is an Object type object, so you can add any arbitrary Properties and methods allow instance objects to inherit them

For example: The prototype of a String type object is String.prototype. If we want to add some custom methods to a String type object, then we It can be implemented like this (here is an example of adding a trim method in a class-like VBscript)

Copy code The code is as follows:

String.prototype.trim=function(){
return this.replace(/^s*|s*$/g,"")
}
// " jiangsk540 ".trim();//return "jiagnsk540"

In addition to providing the above features, the prototype also provides a mechanism for a group of similar instance objects to share attributes and methods [which is equivalent to Static properties or static functions, no matter how many instance objects are created using the constructor, the properties and methods defined on the prototype are only defined once from beginning to end. All instance objects share the use of this property or method but it is not the same as C Or the same concept of static properties or static functions of JAVA]
Copy code The code is as follows:

function Class1(name){
this.name = name;
}
Class1.prototype.show=function(){
alert("name=" this.name);
}
var m1 = new Class1("jiangsk540");
var m2 = new Class1("haired lion");
alert(m1.show===m2.show);//Show true

The attributes or methods dynamically added to the constructor prototype can be called immediately by the previously created object
such as
Copy code The code is as follows:

function Class1(name){
this.name = name;
}
Class1.prototype.show=function( ){
alert("name=" this.name);
}
var m1 = new Class1("jiangsk540");
Class1.prototype.say=function(){
alert("Hi");
}
m1.say()//Call successfully
/*
Note: Only properties or methods added to the prototype of the constructor can be used by the created object Like calling immediately
If you change the reference of the constructor prototype, it cannot be called immediately by the created object
*/
Class1.prototype={newP:"jiangsk540"};
alert( m1.newP)//undefined
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