Skip to content
Advertisement

Tag: javascript-objects

Why can I add named properties to an array as if it were an object?

The following two different code snippets seem equivalent to me: and because they both behave the same, and also typeof(myArray) == typeof(myObjects) (both yield ‘object’). Is there any difference between these variants? Answer Virtually everything in javascript is an object, so you can “abuse” an Array object by setting arbitrary properties on it. This should be considered harmful though. Arrays

How do I correctly clone a JavaScript object?

I have an object x. I’d like to copy it as object y, such that changes to y do not modify x. I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted properties. This isn’t a problem, since I’m copying one of my own literal-constructed objects. How do I correctly clone a JavaScript object? Answer

Length of a JavaScript object

I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object? Answer Updated answer Here’s an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other modern ES5+ capable browsers, you can use Object.keys() so the above code just becomes: This doesn’t have to

Advertisement