`

Multiple Inheritance

 
阅读更多
Let's create a multi() function that accepts any number of input objects. You can wrap the loop that copies properties in another loop that goes through all the objects passed as arguments to the function.
function multi() {
var n = {}, stuff, j = 0, len = arguments.length;
for (j = 0; j < len; j++) {
stuff = arguments[j];
for (var i in stuff) {
n[i] = stuff[i];
}
}
return n;
}



Let's test this by creating three objects: shape, twoDee and a third, unnamed object. Creating a triangle object will mean calling multi() and passing all three objects.
var shape = {
name: 'shape',
toString: function() {return this.name;}
};
var twoDee = {
name: '2D shape',
dimensions: 2
};
var triangle = multi(shape, twoDee, {
name: 'Triangle',
getArea: function(){return this.side * this.height / 2;},
side: 5,
height: 10
});

Will it work? Let's see:

>>> triangle.getArea()
25
>>> triangle.dimensions
2
>>> triangle.toString()
"Triangle"

分享到:
评论

相关推荐

    Type.Inheritance.and.Relational.Theory

    Both single and multiple inheritance Scalar, tuple, and relation inheritance Type lattices and union and intersection types Polymorphism and substitutability Compile time and run time binding All of ...

    Selected.Topics.in.Cplusplus.15117

    Then it has multiple inheritance, something that’s not part of most languages. It has pointers and references. You need to know how and when you should use them. Then you have virtual functions and ...

    Learning.Object-Oriented.Programming.1785289632

    With a fair understanding of interfaces, multiple inheritance, and composition, you will move on to refactor existing code and to organize your source for easy maintenance and extension. Learning ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short Functions Google-Specific Magic Smart Pointers cpplint Other C++ Features ...

    A Swift mixin for UITableViewCells and UICollectionViewCells.zip

    A Swift mixin for UITableViewCells and UICollectionViewCells.zip,A Swift mixin for reusing views easily and in a type-safe way (UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers...

    002-A Beginners Guide to Python 3 Programming.pdf

    Topics such as generators and coroutines are often misunderstood and these are explained in detail, whilst topics such as Referential Transparency, multiple inheritance and exception handling are ...

    面向对象编程:C++与Java比较教程 英文版

    Chapter 16 - Multiple Inheritance in C++ Chapter 17 - OO for Graphical User Interfaces, A Tour of Three Toolkits Chapter 18 - Multithreaded Object-Oriented Programming Chapter 19 - Network Programming...

    Learning Object-Oriented Programming(PACKT,2015)

    With a fair understanding of interfaces, multiple inheritance, and composition, you will move on to refactor existing code and to organize your source for easy maintenance and extension. Learning ...

    Fluent Python

    Learn how to write ... •Examine attribute descriptors and when to use them: the key to ORMs, •Explore Pythonic objects: protocols versus interfaces, abstract base classes and multiple inheritance

    fluent python

    Learn how to write ... •Examine attribute descriptors and when to use them: the key to ORMs, •Explore Pythonic objects: protocols versus interfaces, abstract base classes and multiple inheritance

    面向对象编程:C++与Java比较教程 英文精简版

    Chapter 16 - Multiple Inheritance in C++ Chapter 17 - OO for Graphical User Interfaces, A Tour of Three Toolkits Chapter 18 - Multithreaded Object-Oriented Programming Chapter 19 - Network Programming...

    Scala in Depth

    Scala's OO features—type member inheritance, multiple inheritance, and composition Functional concepts and patterns—immutability, applicative functors, and monads ===================================...

    Object Oriented Programming with Swift 2(PACKT,2016)

    After that, you’ll discover the power of parametric polymorphism and will combine generic code with inheritance and multiple inheritance. Later, you move on to refactoring your existing code and ...

    scala in depth

    -type member inheritance, multiple inheritance, and compositionFunctional concepts and patterns--immutability, applicative functors, and monads ==================================================Table ...

    C++ Quick Syntax Reference

    Some of the major additions to C include object-orientated programming, operator overloading, multiple inheritance and exception handling. The development of C++ began in 1979, seven years after C ...

    Swift 3 Object Oriented Programming, 2nd Edition [PDF]

    You'll combine generic code with inheritance and multiple inheritance. Later, you'll see how to combine functional programming with object-oriented programming and find out how to refactor your ...

Global site tag (gtag.js) - Google Analytics