开发者

How do i wrap a javascript object literal in a YUI Object wrapper?

Ok, I know this is a stupid question, and stupid quesitons are most commonly the hardest to find answers for as everyone assumes they are just known. Anyway, i have this:

var map = {
    "key1" : "someValue1",
    "key2" : "someValue2"
};

And then I enumerate the entries thusly:

Y.Object.each( map, function(value,key,object) {
    // do something;
} );

But what I really want to do is something like:

map = Y.Object.wrap(map);
map.each( function(value,key,object) {
} );

It is worth noting that the original object may be passed in as an argument, so I will not always be constructing them. In other words, i really do want to wrap one (and in a way that will not double wrap if it is already wrapped). The answer is probably obvious and even in the documentation, I just cant find it. So I through myself at the mercy of the googlesphere...

--------------------- EDIT --------------------

When I say wrap, I guess what I mean is (in YUI speak) augment. In other words, I would like the Y.Object methods directly available on the object in question. For example, I want to be able to do something like this:

var map = {
    "key1" : "someValue1",
    "key2" : "someValue2"
};
map.each( function(value,key,object) {
    //do something with each entry in my map
} );

Instead of having to always do this:

var map = {
    "key1" : "someValue1",
    "key2" : "someValue2"
};
Y.Object.each( map, function(value,key,object) {
    //do something with each entry in my map
} );

There are two reasons for this. First, I am lazy and don't want to have to keep typing Y.Object.each( myObject... when all I want is to iterate through each property of my object. Second, I want to be forward compatible so that when the functions each and some get defined natively (wh开发者_如何学Goich I think they already are in firefox and chrome) I can leverage the native implementations without code change. I think the second reason is something Crockford seems to emphasize since he always seems to say that if you want to add a function to the prototype of an Object, do so in a way that will check to see if the function is already defined before doing so. Feel free to correct me if I am wrong anywhere above, and as always, feel free to suggest a solution :)


As best I know, YUI3 doesn't have a wrap function that works for regular objects and adds your desired each() method for them. But, you could make your own like this:

YUI().use('node', function(Y) {

    // code to declare our new wrapper function
    function myWrap(o) {
        if (o instanceof myIterator) return(o);  // don't double wrap
        return(new myIterator(o));
    }

    // code to declare a base class used in the iterator        
    function myIterator(o) {
        Y.mix(this, o);
    }

    // the actual each() iteration function
    myIterator.prototype.each = function(fn) {
        var key;
        for (key in this) {
            if (this.hasOwnProperty(key)) {
                fn.call(this, this[key], key, this);
            }
        }
    }

    // start of regular code that uses the new wrapper
    var map = {
        "key1" : "someValue1",
        "key2" : "someValue2"
    };

    var x = myWrap(map);
    x.each(function(value, key, object) {
        console.log(value);
    });

});

You can see it work here: http://jsfiddle.net/jfriend00/h7Fpp/.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜