Javascript -- prototype

Source: Internet
Author: User
Tags hasownproperty

Each javascript function automatically uses the prototype attribute. You can use prototype to declare common attributes for the class. when an object is created, the constructor will assign the prototype of its attribute to the internal property _ proto _ of the object. In addition, when javascript uses prototype to implement the inheritance mechanism to create a general property and does not use the prototype: copy the code function Fish (name, color) {this. name = name; this. color = color; this. livePlace = "water"; this. canSwim = function () {console. log ("I can swim") ;};} var cod = new Fish ("cod", "white"); var salmon = new Fish ("salmon ", "black"); console. log (cod. livePlace); // Waterconsole. log (salmon. livePlace); // waterconsole. log (salmon. canSwim = cod. canSwim); // false copy the Code. At this time, every time an instance Fish is declared, it will have the same property value livePlace. Some of them are repeated and prototype cannot be used to share methods and properties, the instance can share the property to copy the code function Fish (name, color) {this. name = name; this. color = color;} Fish. prototype. livePlace = "water"; Fish. prototype. canSwim = function () {console. log ("I can swim") ;}; var cod = new Fish ("cod", "white"); var salmon = new Fish ("Salmon", "black"); console. log (cod. livePlace); console. log (salmon. livePlace); console. log (salmon. canSwim = cod. canSwim); // copy the instance sharing attribute livePlace and canSwim prototype and _ proto _ 1 in the code example. all Function _ proto _ points to Function. prototype 2. object _ proto _ points to prototype eg of its constructor: copy the code function Fish (name, color) {this. name = name; this. color = color;} Fish. prototype. livePlace = "water"; Fish. prototype. canSwim = function () {Console. log ("I can swim") ;}; var cod = new Fish ("cod", "white"); var salmon = new Fish ("salmon ", "black"); var object ={}; console. log (Fish. _ proto _ = Function. prototype); // trueconsole. log (cod. _ proto _ = Fish. prototype); // trueconsole. log (object. _ proto _ = Object. prototype); // trueconsole. log (Object. getPrototypeOf (Fish) === Fish. _ proto _); // copy the code to true. Note: objects can be used when the browser does not support getPrototypeOf. g EtPrototypeOf alternative property-related method hasOwnProperty (): whether to include an attribute isPrototypeOf (): whether it is .. in eg: copy the code function Fish (name, color) {this. name = name; this. color = color;} Fish. prototype. livePlace = "water"; Fish. prototype. canSwim = function () {console. log ("I can swim") ;}; var cod = new Fish ("cod", "white"); var salmon = new Fish ("salmon ", "black"); console. log (Fish. prototype. isPrototypeOf (salmon); // trueconsole. log (Cod. hasOwnProperty ("name"); // trueconsole. log ("livePlace" in cod); // true: copy the code and use prototype to implement Inheritance Method 1: copy the code // function People (name, age) of the parent class {this. name = name; this. age = age; this. species = "human"; this. getName = function () {return this. name;} this. getAge = function () {return this. age;} this. sayHello = function () {console. log ("hi, I am the father class") ;}// subclass Children. prototype = new People (); con Sole. log (Children. prototype. constructor = People); // true Children. prototype. constructor = Children; var wish = new Children ("wish"); console. log ("the name of wish is:" + wish. getName (); // the name of wish is: wish console. log (wish. sayHello (); // hi, I am the father class copy code Note: 1. the prototype object contains a contructor attribute. Each instance also has a constructor attribute. The constructor attribute of the prototype object is called by default. Children. prototype = new People (); a new value is assigned to prototype. In this case, Children. prototype. the constructor value is changed to People, so we need to modify it to ensure the correctness of the prototype chain. 2. as long as prototype is modified, you must point the new prototype constructor to the original constructor. view the relevant information of the original constructor: js type judgment and duck type Identification Method 2: Children. prototype = People. prototype; Children. prototype. constructor = Children; note: In this mode, if Children. people. prototype will also be changed accordingly

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.