What you need is the non - enumerable properties of the object (and, possibly, those that it inherits from its prototype). I do not believe that there is a standard way to get them through JavaScript.
If you use a debugger and validate an object, usually all properties of the object (and not just enumerated ones) are displayed. Now all major browsers have built-in debuggers: Chrome has Dev tools (Ctrl + Shift + I); IE8 and above have "F12 Developer Tools"; IE7 and earlier can be debugged through the free version of VS.Net; the latest versions of Firefox have built-in tools, for older versions you can get the Firebug plugin; Opera has a Dragonfly.
Update . In the comments on the question you said:
I am using Google Chrome 17, and the only property I see with console.log
is __proto__
.
Right {}
has no properties at all, just a prototype. If you click on the small arrow to the left of __proto__
, it will show you the properties of __proto__
. hasOwnProperty
, toString
, etc., all the {}
properties are obtained from the prototype (which is Object.prototype
), and not the properties of the object itself.
JavaScript uses prototypal inheritance, which means the object is supported by the prototype. If you try to get the value of a property that the object does not have, the JavaScript engine will look at the prototype of the object to see if the prototype has this property; if so, then this value is used. If the prototype does not have this, the engine looks at the prototype prototype; and so on, until it reaches the root of the hierarchy. That's why you hear about objects that have their own properties. and properties that they inherit.
Here is an example:
Here is the constructor function. We put the property in a prototype that will use the JavaScript engine if we use new Foo
to create the object.
function Foo() { } Foo.prototype.bar = 42;
Create an object using this constructor:
var f = new Foo();
f
has no properties at all, and yet:
console.log(f.bar);
... because since f
does not have a property called "bar", the engine looks at the prototype f
, which is the Foo.prototype
object.
Now let's f
own the "bar" property:
f.bar = 67; console.log(f.bar);
Now let's remove the f
property "bar":
delete f.bar;
What happens if we try to restore f.bar
now?
console.log(f.bar);
If you said 42
, you will get top marks. Since f
no longer has a property called "bar", we return to its prototype.
Please note that this is a live ratio, therefore:
Foo.prototype.bar = 96; console.log(f.bar);
In the third edition of ECMAScript (most browsers implement something in accordance with the 3rd edition), the only way to assign a prototype to an object is to use the property of the prototype
constructor function, as described above. With the 5th edition, a more direct way was added: Object.create
, which can be passed directly to the prototype object:
var proto = {bar: 42}; var obj = Object.create(proto); console.log(obj.bar);