Am not new to JS or its syntax, but sometimes, the semantics of the language has me stumped at times. At work today, a colleague mentioned this: is not the same as or since the first version actually assigns the reference to an empty array to a and b. I couldn’t quite accept this as true, but I’m not sure.
Tag: object
How to determine whether an object has a given property in JavaScript
How can I determine whether an object x has a defined property y, regardless of the value of x.y? I’m currently using but that seems a bit clunky. Is there a better way? Answer Object has property: If you are testing for properties that are on the object itself (not a part of its prototype chain) you can use .hasOwnProperty():
Sorting a JavaScript object by property name
I’ve been looking for a while and want a way to sort a Javascript object like this: and sort is alphabetically by name to get: I can’t find any code that will do this. Can anyone give me some help? Answer UPDATE from the comments: This answer is outdated. In ES6 objects keys are now ordered. See this question for
Merge keys array and values array into an object in JavaScript
I have: And I’d like to convert it into this object: In Python, there’s the simple idiom dict(zip(keys,values)). Is there something similar in jQuery or plain JavaScript, or do I have to do this the long way? Answer Simple JS function would be: Of course you could also actually implement functions like zip, etc as JS supports higher order types
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 to determine equality for two JavaScript objects?
A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java? Stack Overflow question Is there any kind of hashCode function in JavaScript? is similar to this question, but requires a more academic answer. The scenario above demonstrates