1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <MetaCharSet= "UTF-8">5 <title>JS Advanced Syntax 3-prototype mode</title>6 </Head>7 <Body>8 <Script>9 /*Ten JavaScript prototype mode (prototype): One 1. A prototype is an object that other objects can implement to inherit property, and all objects have a prototype by default. A because the prototype itself is also an object. So a real prototype of a class is held by the prototype property of the class's interior. - 2. What can be called an object? - in JavaScript, an object is a collection of any unordered key-value pairs. the if he's not a master data type (undefined,null,boolean,number,string) - All the others are called objects. - */ - + /* - the prototype in JavaScript is closely connected with the function; + var o ={} He's not defined by function, does he have a prototype? A there is. at each object generated through the function and the new operator holds an attribute __proto__, which - a prototype reference to the prototype that created his constructor was saved. - * */ - /* - function Person () {}//define an empty object - person.prototype.name= "Zhang San"; in person.prototype.showname=function () { - //This represents a specific instantiated class that invokes this function to alert (this.name); + } - new Person (). ShowName (); the * $ var cat = {};Panax Notoginseng //cat.prototype.name= "112";//error, prototype undefined - //default hidden call the following code: the object.getprototypeof (cat). Name= "Zhangdan";//This is the way to get hidden prototypes, browser support is not good + Cat.__proto__.master = "JavaScript"; A //The upper two are explicit calls, the following are implicit calls, the effect is the same the cat.age=2; + cat[' sex ']= "man"; - $ alert (cat.name+ "+cat.master+" "+cat.age+" "+cat[" Sex "]); $ */ - //simple inheritance with prototype mode - functionper () { the This. GetName= function(str) { - alert (str);Wuyi } the } - Per.prototype.getAge= function(age) { Wu alert (age); - } About //Not fully inherited $ vara= {};//Empty Class - a.__proto__=Per.prototype; - //reference the prototype to be inherited, assigning to the class to inherit, equivalent to a Mount - //if the class to be inherited is an empty class, then it is fully inherited A A.getage (3); + //A.getname ("Zhang Dan");//This method is an error, because the inheritance is the prototype, that is, all things on the upper level, the //but does not include properties and methods that are defined by the inherited class itself - $ the //simple way to implement inheritance (JS cannot implement multiple inheritance) the //Full Inheritance the varb= {}; the b.__proto__= Newper (); - B.__proto__.constructor=b; in B.getage (9); the B.getname ("Xiaowang"); the About //Concatenation inheritance the functionm () { the This. SHOWM= function(){ the Alert ("I am is M") + } - } the functionN () {Bayi This. Shown= function(){ the Alert ("I am is N") the } - } - the functionK () {}; the N.prototype= Newm (); the N.prototype.constructor=N; the - K.prototype= Newn (); the K.prototype.constructor=K; the varBoo= NewK (); the boo.showm ();94 Boo.shown (); the </Script> the </Body> the </HTML>
JavaScript design mode-3. Prototype mode