In JavaScript, what is the difference between an object and a hash? How do you create one vs the other, and why would you care? Is there a difference between the following code examples?
var kid = { name: "juni", age: 1 }
And:
var kid = new Object(); kid.name = "juni"; kid.age = 1;
And:
var kid = new Object(); kid["name"] = "juni"; kid["age"] = 1;
Can you think of any other code example I should illustrate?
The core question here is what is the difference between an object and a hash?
A hash object is dynamically created in memory at run-time. The size of a hash object grows as items are added and it contracts as items are removed. A hash object consists of key columns, data columns, and methods such as DECLARE, FIND, etc. A hash object's scope is limited to the DATA step in which it is created.
The key in a hashmap can be any datatype, this includes arrays and objects. Meanwhile, objects can only use integers, strings, and symbols as their keys. Hashmaps are organized as linked lists, so the order of its elements is maintained, which allows the hashmap to be iterable.
Yes, we can use any object as key in a Map in java but we need to override the equals() and hashCode() methods of that object class.
With arrays, the key is an integer, whereas hashes support any object as a key. Both arrays and hashes grow as needed to hold new elements. It's more efficient to access array elements, but hashes provide more flexibility.
There just isn't any. All three of those are literally equal.
They are different notation systems that you can use interchangeably. There are many situations where using the bracket syntax [ ]
can be more appealing, an example would be when referencing an object with a variable.
var temp = "kid"; var obj = new Object(); obj[temp] = 5; // this is legal, and is equivalent to object.kid obj.temp = 5; // this references literally, object.temp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With