tmim tmim - 1 year ago 35
Javascript Question

Is it possible to create a hidden property in javascript

I want to create an object with a hidden property(a property that does not show up in a

for (var x in obj
loop). Is it possible to do this?


It isn't possible in ECMAScript 3 (which was what the major browsers implemented at the time this question was asked in 2010). However, in ECMAScript 5, which current versions of all major browsers implement, it is possible to set a property as non-enumerable:

var obj = {
   name: "Fred"

Object.defineProperty(obj, "age", {
    enumerable: false,
    writable: true

obj.age = 75;

/* The following will only alert "name=>Fred" */
for (var i in obj) {
   window.alert(i + "=>" + obj[i]);

This works in current browsers: see for details of compatibility in older browsers.

Note that the property must also be set writable in the call to Object.defineProperty to allow normal assignments (it's false by default).