<script type="text/javascript">Object.prototype.size = function () {var len = this.length ? --this.length : -1;for (var k in this)len++;return len;}Object.prototype.size2 = function () {var len = this.length ? --this.length : -1;for (var k in this)len++;return len;}var myArray = new Object();myArray["firstname"] = "Gareth";myArray["lastname"] = "Simpson";myArray["age"] = 21;alert("age is " + myArray["age"]);alert("length is " + myArray.size());</script>
function uberject(obj){this._count = 0;for(var param in obj){this[param] = obj[param];this._count++;}}
uberject.prototype.getLength = function(){return this._count;};
var foo = new uberject({bar:123,baz:456});alert(foo.getLength());
var myObject = {}; // ... your object goes here.
var length = 0;
for (var property in myObject) {if (myObject.hasOwnProperty(property)){length += 1;}};
console.log(length); // logs 0 in my example.
// Count the elements in an objectapp.filter('lengthOfObject', function() {return function( obj ) {var size = 0, key;for (key in obj) {if (obj.hasOwnProperty(key)) size++;}return size;}})
// case 1var a = new Object();a["firstname"] = "Gareth";a["lastname"] = "Simpson";a["age"] = 21;
//case 2var b = [1,2,3];
// case 3var c = {};c[0] = 1;c.two = 2;
function objLength(obj){return Object.keys(obj).length;}
console.log(objLength({a:1, b:"summit", c:"nonsense"}));
// Works perfectly finevar obj = new Object();obj['fish'] = 30;obj['nullified content'] = null;console.log(objLength(obj));
// It also works your way, which is creating it using the Object constructorObject.prototype.getLength = function() {return Object.keys(this).length;}console.log(obj.getLength());
// You can also write it as a method, which is more efficient as done so above
Object.defineProperty(Object.prototype, "length", {get:function(){return Object.keys(this).length;}});console.log(obj.length);
// probably the most effictive approach is done so and demonstrated above which sets a getter property called "length" for objects which returns the equivalent value of getLength(this) or this.getLength()
function Person(name, age){this.name = name;this.age = age;}
Person.prototype.getIntro= function() {return `${this.name} is ${this.age} years old!!`}
let student = new Person('Anuj', 11);
console.log(Reflect.ownKeys(student).length) // 2console.log(student.getIntro()) // Anuj is 11 years old!!