1

I'm tired of looping through an object literal to get its length for one reason or another. I want to define a length property onto Object.prototype, I can set the initial length, but I don't know how to update it when the object is modified?

var obj = {a:1, b:2, c:3};
Object.defineProperty(Object.prototype, 'length', {value: 0});

//  obj.length === 0;

obj.d = 4;

//  obj === {a:1, b:2, c:3, d:4};
//  obj.length === 0;

Any String/Array object does this automatically, if you know how this happens then please enlighten me, if not then maybe you know a way to update this custom property?

alex
  • 479,566
  • 201
  • 878
  • 984
LostInCyberSpace
  • 413
  • 4
  • 19

3 Answers3

3

If you want the length of the number of keys in an object, use Object.keys(obj).length.

If you're crazy and want it on the prototype...

Object.prototype.getKeyLength = function() {
    return Object.keys(this).length;
};

Any String/Array object does this automatically, if you know how this happens then please enlighten me...

These are host objects/primitives and it happens behind the covers.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Ok dude cheers for the heads up on the `keys` method, but `getKeyLength` is a function and therefore needs to be called to update the property... that means I'd have to `ovserve` the object for changes and then call the function – LostInCyberSpace Jun 24 '15 at 03:34
2

Your init guess is close enough, from Object.defineProperty()

You can use use get instead of value, to define a getter, which be called when the defined property is accessed.

A Demo

Object.defineProperty(Object.prototype, 'len', {
  writeable: false,
  get: function() {
    return Object.keys(this).length;
  }
});

var a = {
  't' : 'This',
  's' : 'A tsets'
};

console.log(a.len);

a['2222'] = 3;

console.log(a.len);
fuyushimoya
  • 9,715
  • 3
  • 27
  • 34
0

You can try this:

Object.defineProperty(Object.prototype, 'keylength', {
  get: function getKeyLength() {
    return Object.keys(this).length;
  }
});
DMaster
  • 603
  • 1
  • 10
  • 23
  • cheers dude, but the whole idea is to look at the chrome console when debugging and seeing 50 property values without having to click 50 `(...)` initiators – LostInCyberSpace Jun 24 '15 at 03:36
  • It seems you need it only for debugging, if you want to easier in this, you may send a request to Chrome's creator. – DMaster Jun 24 '15 at 03:56
  • can you elaborate on that, maybe with a bit of code? – LostInCyberSpace Jun 24 '15 at 04:08
  • This is the only way in ECMAScript 5 and ECMAScript 6, Chrome show you `(...)` instead of number of properties because it want to display the object faster (by avoid executing property getter/setter). If there was a feature called `onsetproperty`, everything go easier, but sorry, current ECMAScript standard doesn't support. – DMaster Jun 24 '15 at 04:22
  • Yeah i wish! bring on `Object.onCreated`, and many other Object lifecycle events... – LostInCyberSpace Jul 01 '15 at 19:39