1

I have the following JavaScript:

var djs = function (ob) {

    return {
        remove: function () { //removes element
            if (is_array(ob)) {
                for (var i = 0; i < ob.length; i++) 
                    ob[i].parentNode.removeChild(ob[i]);
            } else {
                ob.parentNode.removeChild(ob);
            }
        },
        empty: function () { //makes element empty
            if (is_array(ob)) {
                for (var i = 0; i < ob.length; i++) 
                    ob[i].innerHTML = "";
            } else {
                ob.innerHTML = ""
            }
        },
        html: function (str) { //gets or sets innerHTML
            if (str) {
                if (is_array(ob)) {
                    for (var i = 0; i < ob.length; i++) 
                       ob[i].innerHTML = str;
                } else {
                    ob.innerHTML = str;
                }
            } else {
                if (is_array(ob)) {
                    for (var i = 0; i < ob.length; i++) 
                        rob += ob[i].innerHTML;
                    return rob;
                } else {
                    return ob.innerHTML;
                }
            }
        }
    }
}

Here every time I am checking whether ob is an array or not and executing code. I want to minimize this, like instead of:

if (is_array(ob)) { 
    for (var i = 0; i < ob.length; i++) 
        ob[i].parentNode.removeChild(ob[i]);
} else { 
    ob.parentNode.removeChild(ob); 
}

I want to use a function like, doEval(ob,code,return), in this case,

doEval(ob,"parentNode.removeChild("+ob+")",NULL);

"return" parameter will return if I specify any like innerHTML. Can any one help?

2
  • 1
    It seems you have a PHP accent Commented Feb 27, 2011 at 19:49
  • I think you could do this with a higher-order function, but Javascript also has an eval function. Commented Feb 27, 2011 at 19:49

3 Answers 3

5

Don't repeat is_array check:

var djs=function(ob) {
  if (!is_array(ob)) ob = [ob];
Sign up to request clarification or add additional context in comments.

Comments

0

@SHiNKiROU is right of course, but just to provide an example of how to solve your problem with higher-order functions:

function doToAll(ob, callback) {
    if(is_array(ob)) {
        for (var i = 0; i < ob.length; i++) {
            callback(ob[i]);
        }
    } else {
        callback(ob);
    }
}

...

remove:function(){ //removes element
    doToAll(ob, function(actualOb) { actualOb.parentNode.removeChild(actualOb); });
},
...

But again, use @SHiNKiROU:s answer for this particular case.

Comments

-1

Try this:

function doEval(a, b, c) {
    if(is_array(a)) {
        eval(b);
    } else {
        eval(c);
    }
}

NULL doesn't exist by the way, it is null.

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.