This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 6 years ago.
I was going through Map Documentation on MDN. In Examples, under Using Map Object, Object Literal - {} is used as key to store value. But, the value in Map can't be retrieved using Object Literal.
I verified this in Browser Console and found that Object Literal is not equal to itself. Also, the Function Expression - function() {} is not equal to itself.
I couldn't find the reason behind this. If required, I can ask a different question for Function Expression.
Each time you do {}, it creates a new empty object, so when you do {} == {}, you're comparing two different objects. This comparison is done by reference, so it returns false.
Related
This question already has answers here:
How do I empty an array in JavaScript?
(17 answers)
Closed 14 days ago.
I am trying to extend the Array object in Javascript exactly like this. I am trying to manipulate the values of the array (present in the this object) using the prototype like in the function.
function clear() {
this = [];
}
Object.defineProperty(Array.prototype, 'clear', { value: clear, enumerable: true, });
It is giving me a error
Uncaught SyntaxError: Invalid left-hand side in assignment
I am aware that the this is immutable. What is the workaround? I am not looking to create a new Object that emulates the Array constructor.
One option would be
this.splice(0, this.length)
I guess you can also do this.length=0 directly, but it feels kinda hacky.
This question already has answers here:
Check if a value is an object in JavaScript
(54 answers)
Closed 7 years ago.
Is it possible to test a variable to see if it is a primitive?
I have seen lots of questions about testing an variable to see if it is an object, but not testing for a primitive.
This question is academic, I don't actually need to perform this test from my own code. I'm just trying to get a deeper understanding of JavaScript.
To test for any primitive:
function isPrimitive(test) {
return test !== Object(test);
}
Example:
isPrimitive(100); // true
isPrimitive(new Number(100)); // false
http://jsfiddle.net/kieranpotts/dy791s96/
Object accepts an argument and returns if it is an object, or returns an object otherwise.
Then, you can use a strict equality comparison, which compares types and values.
If value was an object, Object(value) will be the same object, so value === Object(value). If value wasn't an object, value !== Object(value) because they will have different types.
So you can use
Object(value) !== value
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I need to know why this isn't working. My Javascript code accesses a variable in an object. But it appears not to be working, partly because I can't figure out the syntax.
var obj = {
size:"small",
big:false,
thing:true
}
alert(obj[size]);
I'm just not sure if I got the syntax right…
This will work here.
obj.size //returns small
OR
obj["size"] //returns small
OR
var my_var = "size"
obj[my_var] //returns small
You can reference object values either by:
obj["size"]
or
obj.size
However, there is an exception. For instance, if you have following object with a number key: (Note: key is still a string even if it's defined this way):
var obj = {
1: true
};
You can retrieve it's value only by using: obj["1"]
Hence, using obj.1 will cause a syntax error.
Therefore, your code works if you change it to e.g.: alert(obj["size"]); but I prefer to use console.log(obj["size"]); for debugging. At least, if you are playing with node.js as your tags indicates.
Cheers.
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 3 years ago.
I was writing a script, and I had something like
var myObject = {};
if (myObject == {}){
...
}
I was surprised to find that this evaluated to false.
My own findings
Reading up on some of the funny querks javascript does, I found this article, which tells that {} will result in a "truthy" object in an if statement, for example
if ({}){ // true
...
}
Hm, well further ready discuesses String Equality, which for object comparison, says it will return true if they reference the same object. (Even {} === {} returns false)
The only reason I can think that is would return false is that the objects are technically at different memory address, and thus considered different objects.
Because every {} creates a unique object. You can't compare them that way.
This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 8 years ago.
A colleague, by mistake wrote this code:
var parameters = [];
// some lengthy code here
parameters.firstParameter = "first parameter value";
parameters.secondParameter = "second parameter value";
He had declared parameters variable as an array, but somewhere else he had used it as an object, adding parameters to it.
The results of testing the type of this parameter is as follow (in Google Chrome's console):
parameters;
// prints []
typeof parameters;
// prints "object"
parameters instanceof Array;
// prints true
So, is it an object or an array at last? Or does it have a dual nature, both array and object at the same time?
In JavaScript everything is an Object. If you define an Array so it'll be treat as an object in JavaScript. Since it is an object, you can access properties associated to it.