Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Code errors in some cases
Message
 
 
To
All
General information
Forum:
Javascript
Category:
Coding, syntax & commands
Title:
Code errors in some cases
Miscellaneous
Thread ID:
01658966
Message ID:
01658966
Views:
31
Hi everybody,

I'm looking at the following code:
 var lastIndex = model.nodes.length - 1;
                    var newNode = model.nodes[lastIndex];
I can see that nodes is an object (in that particular case) and it doesn't have length property, so lastIndex evaluates to NaN and it errors from there.

It only happens when I try to drop a new object on the other object (not into the root object).

The question is - how to code the above defensively so it works when nodes is an array of objects and then it's a single object? Should I first check hasOwnProperty('length')?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform