1

I am trying to simulate multiple inheritance in JavaScript, so I need to find a way to obtain a list of conflicting method names for two JavaScript objects. Is it possible to generate a list of function names for two objects, and then find all of the function names that are the same between the two classes?

    function base1(){
        this.printStuff = function(){
            return "Implemented by base1";
        };
    }

    function base2(){
        this.printStuff = function(){
            return "Implemented by base2";
        };
    }

function getConflictingFunctionNames(object1, object2){
    //get a list of conflicting function names between the two objects. 

    //to be implemented.
}

console.log(getConflictingFunctionNames(base1, base2)); //this should print ["printStuff"]
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • Why would it print `"object1", "object2"`? – Explosion Pills Mar 11 '13 at 01:50
  • I think I'll need to get a list of all methods in each object, as described here, and then find all the method names that are the same in both of them. http://stackoverflow.com/questions/2257993/how-to-display-all-methods-in-a-javascript-object – Anderson Green Mar 11 '13 at 01:50
  • @ExplosionPills I fixed the mistake. I meant to write `"printstuff"`. – Anderson Green Mar 11 '13 at 01:51
  • You don't need to get all the methods of both objects, you just need to get the methods of one object and check which ones the other object has. – RobG Mar 11 '13 at 03:02

2 Answers2

2

You'll need to follow the following steps:

  1. Get all the own property names on each object.
  2. Filter the functions.
  3. Create a union of both the sets.

The first two steps can be combined into a single function:

function getOwnFunctionNames(object) {
    var properties = Object.getOwnPropertyNames(object);
    var length = properties.length;
    var functions = [];

    for (var i = 0; i < length; i++) {
        var property = properties[i];
        if (typeof object[property] === "function")
            functions.push(property);
    }

    return functions;
}

Next you need to find the union of the set of functions of two objects:

function getConflictingFunctionNames(object1, object2) {
    var functions1 = getOwnFunctionNames(object1);
    var functions2 = getOwnFunctionNames(object2);
    var length = functions1.length;
    var functions = [];

    for (var i = 0; i < length; i++) {
        var functionName = functions1[i];
        if (functions2.indexOf(functionName) >= 0)
            functions.push(functionName);
    }

    return functions;
}

Now you may do whatever you wish with these functions.

See the demo here: http://jsfiddle.net/gVCNd/

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
0

If you just want to find the common named methods, then:

function getCommonNamedMethods(obj1, obj2) {
  var result = [];

  for (var p in obj1) {

    if (typeof obj1[p] == 'function' && typeof obj2[p] == 'function' ) {
      result.push(p);
    }
  }
  return result;
}

If you want only own properties of the objects, include an own properties test:

function getCommonNamedMethods(obj1, obj2) {
  var result = [];

  for (var p in obj1) {

    if (obj1.hasOwnProperty(p) && typeof obj1[p] == 'function' &&
        obj2.hasOwnProperty(p) && typeof obj2[p] == 'function' ) {
       result.push(p);
    }
  }
  return result;
}
RobG
  • 142,382
  • 31
  • 172
  • 209