|
|
|
|
|
by cmbaus
4462 days ago
|
|
In JavaScript, like other duck typed languages, you have dynamic dispatch without inheritance. For example: var objA = {doIt:function(){console.log('hello from a')}};
var objB = {doIt:function(){console.log('hello from b')}};
var doItDynamically = function(doitObj) {
doitObj.doIt();
};
doItDynamically(objA);
doItDynamically(objB);
a and b do not share a common class (since classes do not exist in JavaScript), but they implement the same interface. For this reason, they can be used polymorphically, as if they shared a common base class or interface in Java or C++. |
|