1

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

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Shouldn't that be element.id instead of edge.id?
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 :)
The errors will behave the same way standard errors (eg, 1 / 0) behave.
how do these work in a browser? Does the browser just show a pop up window? sounds like you have more questions to ask
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.