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