0
votes

I create an Object in JavaScript:

var object = {value1 : "value1", value2 : "value2"};

If I now try and count the contents of 'object.prototype' I get the result 0. If I add properties I get the correct result. Am I to take it then that 'object.prototype' is in fact empty?

Thanks!

2
In your example I find object.prototype is undefined, and you can't count contents of undefined. There is an object.constructor (which in Chrome is function Object() { [native code] }, and you can check Object.prototype). - nnnnnn
thanks, could you show me what you did? - user1360809
I just pasted your line of code into Chrome's console, then after that I evaluated object.prototype, object.constructor and Object.prototype. Note that you can add properties to Object.prototype, but you shouldn't do so. - nnnnnn
I have never used Chrome console before...will give it a go... - user1360809
There is internal __proto__ in every object .. but its not a valid part of JavaScript, some browsers still has this property. for checking working of this try this .Object.prototype.a = 'value a in native Object' ; and print object.__proto__.a . this link has more info sporto.github.io/blog/2013/02/22/… - rab

2 Answers

0
votes

In Javascript, by DEFAULT only "function objects" have a prototype property..

for example, none of the following have a prototype property by default -

var a = new Object();
var b = {};
var a = new Array();
var b = [];

But,

var a = function() {};

has a prototype property.. and by default -

a.prototype is {constructor : this}
0
votes

If you want to iterate through all the properties you assigned to your object, you can just do it with a for..in loop:

for (var prop in object){
    if (object.hasOwnProperty(prop)){
        var propValue = object[prop];
        // ...
    }
}

If you need to count the properties, you can just increment a counter on each iteration.