1

Javascript Code

var a = {};
a.test += 1; //NaN
++a.test; //NaN

Instead

var a = {};
a.test = 0;
++a.test; //1
a.test += 1; //2

I wonder if there could be anyway that can make first code sample work the same as second, i.e without an explicit assignment to 0. As in assigning default value for any property of an object to 0 instead undefined. I'm trying to do this in node.js. So, no problem of cross browser things and old ECMA Specs.

var i;
for(i = 0; i<10; i++) {
   if(a.test) {
     ++a.test;
   } else {
     a.test = 0;
     ++a.test;
   }
   //a.test = a.test || 0; (1)
   //++a.test;
}

If it is possible then the inner if/else or the assignment statement(1) in the above code can be eliminated.

3
  • Any reason you can't simply use var a = {test: 0};? Commented May 1, 2012 at 9:09
  • @outis properties are dynamically generated. You can even consider var a = []; a[0]++; Here size of the array can't be determined upfront Commented May 1, 2012 at 9:13
  • what should happen if a.test === false or ""? it would be safer to do a.hasOwnProperty("test") if you are trying to establish whether or not the property is present Commented May 1, 2012 at 9:23

6 Answers 6

4

Javascript by default defines all new variables as undefined ( if not explicitly defined ) , which is different from Number object, which you are trying to define. So you should use smth like :

for (i=0; i<10; i++) {
    a.test = a.test || 0;
    ++a.test
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have mentioned ur answer in my code only. See the comment in the 3rd code snippet
You should use prototype/class object if you want your default a.test always defined, as I mention if something isn't defined explicitly as Number, String, etc. it is undefined
3

There's no way to do this for any arbitrary undefined property.

For a known property name, there is a way, but DO NOT USE IT !! 1

Object.prototype.test = 0;

This will give every object an implicit .test property with that value in it, and the first time you attempt to modify it the result will be stored in your own object:

> Object.prototype.test = 0
> a = {}
> a.test++
> a.test
1

1 Adding stuff to Object.prototype will break stuff, including for (key in obj)

Comments

1

This is where prototypes come in useful in javascript:

function Demo() {
}
Demo.prototype.x = 0;

 

> a = new Demo();
> a.x += 1
> a.x
1

 

> b = new Demo()
> b.x
0

1 Comment

@mihai yup, just like my answer, since there's no way do it if you don't know the name of the property
0

In the first code, you can't. Any number added to a non-number or NaN value will always result in NaN

var a = {};
a.test += 1; // undefined + 1 = NaN
++a.test;    // ++(undefined) = NaN

as for the inner if

for(i = 0; i<10; i++) {
   a.test = a.test || 0; //use existing value, or if undefined, 0
   ++a.test;             //increment
}

Comments

0

I think this would do:

var a = {"test":0};
a.test += 1; 

1 Comment

OP does not want to explicitly declare/initialize test
-1

Using standard JS it's not possible to do what you're asking. Unless you prototype I guess? But I'm not experienced with Prototype.And I don't think prototyping works with Node.js

The reason for this is because js is not a typed language (i.e. we only use var to declare a variable, not int a or string b), so in order to use the ++ operator, the variable needs to be given a type through assignment.

hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.