简体   繁体   中英

javascript error: Invalid argument in IE on anonymous function

I'm getting a weird error in internet explorer (I tried on IE 8) Everything is fine with a different browser.

Invalid argument on a function similar to this one (I can't post the original one as it is from a non redistributable library which was originally obfuscated and compressed):

function createADiv() {
    var f = document.createElement('div');
    f.set = [function (z) { // error on this line
        f.style.width = z
    }, function (z) {
        f.style.height = z
    }];
    return f;
 }

The problem lies in the scope of f but I don't get why using f inside the anonymous functions shouldn't work.

Any ideas about how to circumvent this Internet Explorer bug?

I've checked other "invalid argument error" but this one seems to be a different case.

Thanks in advance

I think this is what you are trying to do:

function createADiv() {
    var f = document.createElement('div');
    f.set = {
        width: function (z) { // error on this line
            f.style.width = z
        }, 
        height: function (z) {
            f.style.height = z
        }
    };
    return f;
 }

now when you create a div you can do this:

var newDiv = createADiv();
newDiv.set.height("200px");

but to tell you the truth this is useless because javascript already does this:

newDiv.style.height = "200px";

there is not much difference

Not sure why, try re-writing it as this

function createADiv() {
    var f = document.createElement('div');
    f.set = [function a (z) { // error on this line
        f.style.width = z
    }, function b (z) {
        f.style.height = z
    }];
    return f;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM