<JavaScript> Detailed parasitic inheritance

// prototype inherited and non-inherited prototype combination as one of a combination of inheritance is called inheritance, but inherited this approach is that there is a little flawed, useless subordinate function inherited property, so we have to solve the pollution problem parasitic inheritance ; // create superior constructor - food function food (name, Hot, Taste) { this.name = name; this.hot = Hot; this.taste = Taste;} // to clear the annotation process, we write separately superiors constructor prototype method - modification Food.prototype.bad = function () {the console.log ( "shelf life of 3 days" );}; // Create lower constructor - bread function bread (name, Hot, Taste, Brand) { // here is a function of higher borrowing static property, also known as non-prototypal inheritance Food.apply ( the this , arguments the); // own property this.brand = Brand;} // create an empty function



the Fn = var function () {}; // empty function prototype replaced the upper function prototype, so there is no function of empty private property, and a prototype to eat or others is not the same and parasites do? fn.prototype = Food.prototype; // empty instance of the function assigned to the subordinate function prototype, so there are back to our prototype inheritance, the inheritance is fn prototype, very clean Bread.prototype = new new fn (); // course to restore constructor is still necessary, but can not write here read.constructor = Bread is because Bread.constructor function () {}; Bread.prototype.constructor = Bread; // below to create your own subordinate function prototype method, it is worth noting that expand their prototype prior to the completion of succession, otherwise there will be coverage problems Bread.prototype.dry = function () {console.log ( "one day bite to eat not move!" );} // Now we can create an instance of the test have subordinate elements var bread = new new Bread ( "dirty bag", "1000k", "sweet ", "golden phinex");

Guess you like

Origin www.cnblogs.com/isAndyWu/p/11546727.html