0

I want to add a new method named size to object class in javascript.

var obj = {here: {is: "an"}, object: 2};
object.prototype.size = function() {
var obj=this;
var size = 0, key;
for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
}
return size;
};

console.log(obj.size());

The obj is typeof object, I add new function for object as object.prototype.size=function(){}

But when I run it, the firefox give me error:

ReferenceError: object is not defined (line 10)

you can view the code here:(http://jsbin.com/cisasewo/1/edit)

When I change the method to:

Object.prototype.size = function() {
var obj=this;
var size = 0, key;
for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
}
return size;
};

when it invoke, it just give me the function not the answer the size of the function? why? how to add the size function to object class in javascript?

user504909
  • 9,119
  • 12
  • 60
  • 109

2 Answers2

1

Your second implementation is correct. You just need to change

console.log(obj.size)

to

console.log(obj.size())
mclaassen
  • 5,018
  • 4
  • 30
  • 52
  • There is no `console.log` in the method implementation. `console.log(obj.size)` is a valid statement and will not cause the given error. – zerkms Jun 20 '14 at 02:18
  • @zerkms Look at the jsbin link – mclaassen Jun 20 '14 at 02:19
  • look at the error message on jsbin. As you may find - it has nothing to do with a function call and will persist if you remove the whole `console.log` line – zerkms Jun 20 '14 at 02:20
  • @zerkms it's a valid statement but it's exactly what's causing the output in the 2nd part of the post – mclaassen Jun 20 '14 at 02:20
  • "but it's exactly what's causing the output" --- remove the `console.log` line and check that it's not http://jsbin.com/cisasewo/4 – zerkms Jun 20 '14 at 02:21
  • @zerkms Actually both problems exist – mclaassen Jun 20 '14 at 02:22
  • there is no "both" problem. There is one - the wrong `object` name – zerkms Jun 20 '14 at 02:22
  • @zerkms So console.log(obj.size) is invoking the size method? – mclaassen Jun 20 '14 at 02:24
  • it's not. It would output a reference to a function. OP doesn't have problems with that at all. OP didn't ask about how to invoke a method, since they presumably know that – zerkms Jun 20 '14 at 02:25
  • Which is exactly what the poster was experiencing in the 2nd part where they were using the correct Object with capital O – mclaassen Jun 20 '14 at 02:25
  • Oh god, I did not expect someone would implement JS inheritance without knowing how to call a function :-S – zerkms Jun 20 '14 at 02:26
  • Well I was simply telling them that their 2nd implementation was correct and they (presumably) made the silly mistake of missing the brackets – mclaassen Jun 20 '14 at 02:28
  • Indeed, +1 and dropping my answer. It's silly that question code doesn't match the jsbin one – zerkms Jun 20 '14 at 02:29
0

I think it should be work like this way:

Object.prototype.size = function() {
var obj=this;
var size = 0, key;
for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
}
return size;
};

var t = new Object();
t.a = 1;
alert(t.size());  ///result is 1 because property a is defined
LFF
  • 228
  • 1
  • 4