9

So, here's some sample javascript code:

Object.prototype.simpleFunction = function () {
    return true;
}
var tempObject = {};
for (var temp in tempObject) {
    console.log(temp);
}

Note that if you execute this, you'll get 'simpleFunction' output from the console.log commands in Google Chrome. (I'm using 19.0.1084.46m .)

However, the wide variety of related Object functions are not passed to the console.log.

How can I add functions onto the Object prototype without them showing up in my 'for property in object' loops?

Edit: I should have mentioned that the last thing I wanted was to throw another 'if' statement in there, as it'd mean I'd need to add it to ALL for loops. :(

SoreThumb
  • 530
  • 1
  • 5
  • 17
  • See [How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop](https://stackoverflow.com/q/13296340/1048572) – Bergi Apr 19 '19 at 13:47

4 Answers4

15

Which is why you should always check hasOwnProperty:

for (var temp in tempObject) {
    if (Object.prototype.hasOwnProperty(tempObject, temp)) {
        console.log(temp);
    }
}

Crockford advocates using Object.prototype.hasOwnProperty instead of tempObject.hasOwnProperty, just in case you override hasOwnProperty in your object.


In ES5, you can set it to not be enumerable:

Object.defineProperty(Object.prototype, 'simpleFunction', {
    value: function() {
        return true;
    },
    enumerable: false, // this is actually the default
});

Alternatively (in ES5), you can use Object.keys() to only get the object's own keys:

Object.keys(tempObject).forEach(function(key) {
    console.log(key);
});
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • I was looking up enumerable: http://stackoverflow.com/questions/8918030/is-it-possible-to-emulate-non-enumerable-properties : It looks like that enumerable is the answer, even if it's not widely supported yet. I'll be checking compatibility while I work in development. Thanks! :) – SoreThumb May 22 '12 at 04:07
0

Do you mean something like:

for (var temp in tempObject) {
    if (tempObject.hasOwnProperty(temp )) {
         console.log(temp);
    }
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

You can skip the inherited properties by doing this:

if (tempObject.hasOwnProperty(temp)) {
    // property didn't come from the prototype chain
}

The bottom line is, you can't add functions to the prototype without having them being iterated using in.

You could define an external interface in which you always pass the object, e.g.

function simpleFunction(obj) {
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

It's not possible to do this in javascript. You need to filter the results yourself. One potential method is to define your own prototype properties in another object:

var myObjectExt = {
    sampleFunciton: function(){}
}
var p;
for(p in myObjectExt){
    Object.prototype[p] = myObjectExt[p];
}

var obj = {};
for(p in obj){
    if(myObjectExt[p])continue;
    //do what you need
}
supNate
  • 432
  • 3
  • 10
  • This is a pretty good wrapper function in case I need to add more than one function to Object.prototype and can't use `enumerable`. :) Thanks! – SoreThumb May 22 '12 at 04:14