Javascript Learning (5) -- [basic review] prototype of class _ 2 _

Source: Internet
Author: User
The prototype of the javascript class. Here I would like to put it simply: javascript itself does not have strict Inheritance like java. We say that javascript inheritance, it is also similar to the object-oriented language, so although javascript supports object-oriented programming, we do not use the prototype of the javascript class. I would like to briefly describe it here, javascript itself does not have the same strict inheritance as java. We say that the inheritance of javascript is also imitated by the object-oriented language. Therefore, although javascript supports object-oriented programming, but we don't think javascript is a good object-oriented programming language. How is javascript object-oriented implemented? First, let's briefly describe that prototype can be called directly by each class. This can be said to be the prototype of the javascript class. Of course, this involves the design pattern of the prototype chain, of course, we will introduce it later. _ proto _ is the prototype that each object can call directly. So what are the benefits of prototype?
/*** Javascript prototype ** 1. A prototype is an object. Other objects can inherit attributes through it. * all objects have a prototype by default, because the Prototype itself is an object, the real * Prototype of a class is held by the [Prototype] attribute inside the class. * 2. what can be called an object * in javascript, an object is a set function of any unordered key-Value Pair | var a ={} * if it is not a primary data type (undefined, null, boolean, number, String) * Other common objects are called objects */(function () {/*** prototype in javascript is closely connected with functions) * var o = {} doesn't he use function. Does he have the original type? * Answer: * required. * each object generated using the new operator holds an attribute _ proto __, * This property stores the reference of the prototype that created the constructor */function person () {}// defines an empty object person. prototype. name = "USPCAT. COM "; person. prototype. showName = function () {// this indicates a specific instantiated class alert (this. name);} new person (). showName (); var cat ={}; // cat empty class // call the following code Object by default hidden. getPrototypeOf (cat ). name = "MAOMI"; // using the getPrototypeOf function, you can directly obtain the cat of the previous prototype chain of the object. _ proto __. master = "USPCAT. COM "; // test, // implicitly call the above method cat. age = 2; cat ["sex"] = "MAN"; alert (cat. name + "" + cat. age + "" + cat ["sex"] + "" + cat. master)/*** use the prototype mode to implement the inheritance seen **/function per () {this. getName = function (str) {alert (str)} per. prototype. getAge = function (age) {alert (age)} var a = {}; // null Class a is object. _ proto _ = per. prototype;. getAge (2); //. getName ("YUNFENGCHENG")/*** simple inheritance * multiple inheritance cannot be implemented in JS */var B ={}; B. _ proto _ = new per (); // change the constructor function B. _ proto __. constructor = B; B. getAge (1) B. getName ("JAVASCRIPT") // class a {//} // class B extend A {// public B () {// super () //}/*** series inheritance */function m () {this. showM = function () {alert ("im is m")} function n () {this. showN = function () {alert ("im is n")} function k () {}; n. prototype = new m (); n. prototype. constructor = n; k. prototype = new n (); k. prototype. constructor = k; var boo = new k (); boo. showM (); boo. showN ();})()

 

In fact, the benefit is that we can implement inheritance. In other words, we can point the prototype of this class to an object or the prototype of an object. In terms of understanding, we can understand that we direct the reference of this object to the object on the right, so we can better understand it. Some diagrams will be provided in conjunction with the code for your understanding, if you do not understand it, leave a message to discuss it. The code and comments are as follows:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.