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/
精彩评论