开发者

Hook constructor Object?

What I'd like to do is:

1. do something with Object (overwrite or set some 开发者_高级运维property of it)

2. when I define an object like {'a': 12}, alert "'a':12"

Is it possible?


You can do this:

var obj = {'a': 12};

for (ind in obj ){
   alert("'"+ind+"': "+obj[ind]);
}

Fiddle: http://jsfiddle.net/maniator/nwrWq/


if (typeof Object.make !== 'function') {
    Object.make = function (o) {
        for(key in o) {
            if(o.hasOwnProperty(key))
                alert( key + ' ' + o[key]);   
        }
        return o;
    };
}

var MyObj = Object.make({ a: 1});

http://jsfiddle.net/uc5Pj/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜