开发者

Hook constructor Object?

开发者 https://www.devze.com 2023-03-14 20:55 出处:网络
What I\'d like to do is: 1. do something with Object (overwrite or set some 开发者_高级运维property of it)

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

精彩评论

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