Programmatically Determining when a Variable is a Set [duplicate] - javascript

This question already has answers here:
How to check object is a Set? [duplicate]
(3 answers)
How to reliably check an object is an EcmaScript 6 Map/Set?
(5 answers)
Closed 3 years ago.
Is it possible to determine whether a variable is a Set of not in NodeJS?
That is, this code
let thing1 = new Set([1,2,3])
let thing2 = [1,2,3]
console.log(typeof thing1)
console.log(typeof thing2)
reports that both variables contain an object
object
object
Is there a built-in way to determine if one variable contains a set or not? If not, is there a common heuristic used to determine if a variable is a set or not?

Have you tried instanceof?
let thing1 = new Set([1,2,3])
let thing2 = [1,2,3]
console.log(thing1 instanceof Set)
console.log(thing2 instanceof Set)

Related

Difference between types of object creation in javascript [duplicate]

This question already has answers here:
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 4 years ago.
What's the difference between
var obj = new factoryCreation;
and
var obj = new factoryCreation();
in javascript.
Are these one and the same??
Assume factoryCreation is a function.
The MDN Web Docs says:
new Foo is equivalent to new Foo(), i.e. if no argument list is specified, Foo is called without arguments.

Object Comparison with same value [duplicate]

This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
Closed 7 years ago.
I am checking obeject comparison, below two instance gives 'false' and anyone can explain why?
var a = {x:7, y:9};
var b = {x:7, y:9};
console.log(a==b); //false
console.log(a===b); //false
a is not a primitive value. a is just refers to an object and b refers to another object. Both are holding reference of different object.
Both can not be same

Deep setting of object key-values in JavaScript [duplicate]

This question already has answers here:
How to set object property (of object property of..) given its string name in JavaScript?
(16 answers)
Dynamic deep setting for a JavaScript object [duplicate]
(3 answers)
Closed 8 years ago.
Consider the following code:
var obj = {};
obj.bar = 'w00p!'; //works
obj.foo.bar = 'there it is'; //throws error because bar is not set
Could you ever make obj.foo.bar = work if obj.foo is not set?
Is there an elegant way to set a deep variable in an object in JavaScript? In the same spirit as mkdir's -p option: mkdir -p a/deep/dir

What does window['someKeyword'] mean in JavaScript? [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
What does window['someKeyword'] mean in JavaScript?
I guess the window is the DOM window object, but the syntax to retrieve a property is window.propertyName in the reference I found, not window['propertyName']. Are these two ways to retrieve the property?
To access a field of an object in Javascript there are two ways.
let's this is an object
obj = {name:'Object1'};
You can access name field by two ways
obj.name // returns 'Object1'
obj['name'] //returns 'Object1'
besically [] this method is useful when you have to access a field using a variable like bellow
var obj = {name:'Object1'}
var field = 'name'
obj[field] //returns 'Object1' because it will put value of field in `[]`

How can I tell the difference between a regular object and a jquery object (page element)? [duplicate]

This question already has answers here:
Check if object is a jQuery object
(9 answers)
Closed 9 years ago.
// var = {hey: "baby"};
// or
// var = $('#thingy');
if(typeof var == 'object'){ // this is true for both =(
}
I need my code to be smart enough to be able to tell the difference between these two. What's the best way to accomplish that?
You can use the instanceof operator for this.
obj instanceof jQuery
In addition, I don't think you want to use "var" as a variable name. It's reserved to establish variable scope.

Categories