several way to implement inheritance in javascript
2013-05-14 23:45
399 查看
//1.prototype chain inheritance function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function () { return this.property; }; function SubType() { this.subproperty = false; } //inherited from SuperType SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function () { return this.subproperty; }; //2.constructor stealing inheritance function SuperType() { this.colors = ["red","blue","green"]; } function SubType() { //inherited from SuperType SuperType.call(this); } //3.combination inheritance function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function () { alert(this.name); }; function SubType(name,age) { //inherit properties SuperType.call(this, name); //second call superType() this.age = age; } //inherit method SubType.prototype = new SuperType(); //first call superType() SubType.prototype.sayAge = function () { alert(this.age); }; //4.prototypal inheritance in javascript function object(o) { function F() { } F.prototype = o; return new F(); } //5.parasitic inheritance function createAnother(original) { var clone = object(original); clone.sayHi = function () { alert("hi"); }; return clone; } //6.parasitic combinate inheritance function inheritPrototype(subType, superType) { var prototype = object(superType.prototype); prototype.constructor = subType; subType.prototype = prototype; } function SuperType(name) { this.name = name; this.colors = ["red","blue","green"]; } SuperType.prototype.sayName = function () { alert(this.name); }; function SubType(name, age) { SuperType.call(this, name); this.age = age; } inheritPrototype(SubType,SuperType); SubType.prototype.sayAge = function () { alert(this.age); };
相关文章推荐
- What is the difference between the ways to implement inheritance in javascript.
- several way to create object in javascript
- How to implement inheritance in JavaScript
- 【MyEcplise】导入项目后,会定时弹出一下错误MyEcplise tern was unable to complete your request in time.This couble happen if your project contains several large javaScript libraies.
- How to implement xpcom module in JavaScript
- Object.create(): the New Way to Create Objects in JavaScript
- What is the best way to implement a heartbeat in C++ to check for socket connectivity?
- How to implement scrolling image with javascript in web page
- 【转】Any way to implement BLE notifications in Android-L preview----不错
- Easy way to implement Asynchronized Product Filtering(Layered Navigation) in Magento
- A Way to implement Abstract Class In Flex
- What's the best way to define a class in javascript
- A Way to implement Abstract Class In Flex
- Best Way to Loop Through an Array in JavaScript
- Simple way to implement caching in ASP.NET Web API
- A way to implement a general purpose LinkedList in C
- [cross domain] four approachs to cross domain in javascript
- A Simple Algebraic Way to understand Quaternions and Rotations in 3D
- Nginx启动报错:10013: An attempt was made to access a socket in a way forbidden
- How to get multiple select value both in javascript and php