Home  >  Article  >  Web Front-end  >  Reference for some example methods and properties inherited in JavaScript_javascript skills

Reference for some example methods and properties inherited in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:21:421002browse
The role of the prototype attribute:
Use the prototype attribute to provide a set of basic functions of the object's class. New instances of an object "inherit" the operations assigned to the object's prototype.

Function of the prototype attribute:
All JavaScript internal objects have a read-only prototype attribute. Functionality can be added to the prototype of an internal object, but the object cannot be given a different prototype.
However, user-defined objects can be assigned to new prototypes.

The role of the constructor attribute:
constructor represents a function that creates an object.

Function of the constructor property:
The constructor property is a member of all objects with prototype. They include all JavaScript internal objects except Global and Math objects.
The constructor attribute holds a reference to a function that constructs a specific object instance.

A Use prototype to add attributes of objects [Method 1]
Example:
Copy code The code is as follows :




B Use prototype to add attributes of objects [Method 2]
Example:
Copy code The code is as follows:




C uses prototype to inherit the prototype attributes of the parent class
Example:
Copy the code The code is as follows:

<script> <br>function Person(_name){ <br>this.name = _name; <br>} <br>//Create object (used to change prototype prototype object) <br>function addSex(_sex){ <br>this.sex = _sex; <br>} <br>//Change the prototype object <br>Person.prototype = new addSex('male'); <br>var p = new Person( 'xugang'); <br>alert("The prototype of p is: " p.constructor); <br>//Print all attributes<br>for(var i in p){ <br>//alert(p[ i]); <br>} <br><br>// ================= Inheritance================ = <br>//Create the sub-object Student <br>function Student(_study){ <br>this.study = _study; <br>} <br>// Let Student inherit Person through prototype <br>Student.prototype = new Person('Andy Lau'); <br>var stu1 = new Student('JS'); <br>alert("The prototype of stu1 is: " stu1.constructor); <br>for(var i in stu1){ <br>alert(stu1[i]); <br>} <br></script>

Because the prototype of the Student object is changed to the Person object, and the prototype of the Person object is changed to addSex , so the prototype of the Student object is addSex.
Note: The prototype of an object is determined at the moment of new object. If it is changed after new object, it will be invalid!

D How to set the prototype object and constructor of an object
Example:
Copy code The code is as follows:



The results are as follows:
b's construction method: B method
b's prototype object's construction method: C method
Attribute: age value: 42
Attribute: name Value: Andy Lau

The __proto__ variable used to save the prototype in the E object
Example:
Copy the code The code is as follows:



The result in Firefox is as follows:
[object Object]
Super_Person method
Super_Person method
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