将函数添加到对象原型的正确方法?

时间:2013-10-18 04:00:51

标签: javascript function constructor prototype

有没有办法以一种不包含在循环中的方式向Object Prototype添加一个函数?

例如:

Object.prototype.stuff = function(){};
var obj = {'hello':1};
for(var i in obj){
    console.log(i);
}
//it will log: hello, stuff
//I'd want it to only log hello,

1 个答案:

答案 0 :(得分:0)

Object.defineProperty(Object.prototype, "stuff", {
    enumerable: false,
    value: function(){
        //...
    }
});