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"
分享到:
相关推荐
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 ...
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 ...
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 ...
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 reusing views easily and in a type-safe way (UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers...
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 ...
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...
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 ...
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
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
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's OO features—type member inheritance, multiple inheritance, and composition Functional concepts and patterns—immutability, applicative functors, and monads ===================================...
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 ...
-type member inheritance, multiple inheritance, and compositionFunctional concepts and patterns--immutability, applicative functors, and monads ==================================================Table ...
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 ...
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 ...