0

window[objref]不工作
self[objref]也不工作

这是jsfiddle

这是代码:

var bob = {
    n: {},
    y: {
        somekey: 12,
        oo: 'yah',
        pop: {
            t: 36
        }
    },
    m: function() {},
    l: 'hi'
};


function membersOf(s) {

    //var o = window[s]; // returns false false
    // var o = self[s]; // returns false false
    var o = s; // returns ["0", "1", "2", "3", "4"] ["somekey", "oo", "pop"]

    var ret_array = [];
    for (var p in o) {
        if (o[p] != o.constructor.prototype[p]) {
            ret_array.push(p);
        }
    }
    if (ret_array.length === 0) {
        return false;
    } else {
        return ret_array;
    }
}

var ref = 'bob.y';
var byname = membersOf(ref);
var byval = membersOf(bob.y);
console.log(byname, byval);
4

1 回答 1

1

你试过var byval = membersOf(eval(ref));吗?

于 2011-06-10T10:17:47.737 回答