views:

46

answers:

2

Can I do the following?

function contains(element) {

 // if the element is a Vertex object, do this
 if (element instanceof Vertex) {

  var vertex = element;
  for ( var index in self.verticies) {
   if (self.verticies[index].id == vertex.id) {
    return true;
   }
  }
  return false;
 }
 // else if the element is an Edge object, do this 
 else if (element instanceof Edge) {

  var edge = element;
  for ( var index in self.verticies) {
   if (self.verticies[index].id == edge.id) {
    return true;
   }
  }
  return false;
 } else {
  // shouldn't come here
  return false;
 }
};

Basically... I want to be able to call contains() and pass it either a Vertex object or an Edge object but I don't want to have duplicate code. Is this the right way to do it? Furthermore, am I handling the assignment var vertex = element / var edge = element correctly? I want to assign element to another Vertex/Edge object and use that for my look up.

Let me know if I need to clarify.

Thanks, Hristo

+3  A: 

Your code should work fine.

Note, however, that there is no point (other than clarity, which is a good thing) in writing var edge = element.
Javascript variables are untyped; there is no difference between edge and element.

Also, you should probably throw an exception instead of

// shouldn't come here
return false;

Finally, why are you searching self.verticies for an Edge?

Note, by the way, that you still have duplicate code.
You can rewrite your function like this:

function contains(element) {
    var searchSet;

    // if the element is a Vertex object, do this
    if (element instanceof Vertex) 
        searchSet = self.verticies;
    else if (element instanceof Edge)
        searchSet = self.edges;
    else
        throw Error("Unexpected argument");

    for (var i = 0; i < searchSet.length; i++) {
        if (searchSet[i].id == element.id) 
            return true;
    }
    return false;
}
SLaks
Shouldn't that be `element.id` instead of `edge.id`?
casablanca
@casablanca: Yes; thanks for catching that.
SLaks
It was a copy/paste mistake. Sorry about that. But thanks for the suggestions. Also, I didn't know I can throw errors JavaScript... how do these work in a browser? Does the browser just show a pop up window? +1 for an excellent response :)
Hristo
The errors will behave the same way standard errors (eg, `1 / 0`) behave.
SLaks
`how do these work in a browser? Does the browser just show a pop up window?` sounds like you have more questions to ask
vol7ron
A: 

Here's an approach that has a couple of advantages:

  1. Smaller functions (no big if/else if chain)
  2. Produces an appropriate error for missing functions without any additional coding

See what you think:

function contains(element) {
    window['contains_' + typeof element](element);
};

contains_string = function(element) {
    alert('string: ' + element);
};

contains('hi!'); // produces alert
contains(3); // error: 'undefined is not a function'

​It has some downsides too.

  1. The error message isn't terribly informative (not much worse than default behavior though)
  2. You 'pollute' the 'window' object here a little (it'd work better as part of an object)
  3. etc
sje397