Is there a difference between {} and new Object() [duplicate] - javascript

This question already has answers here:
What is the difference between `new Object()` and object literal notation?
(12 answers)
Closed 1 year ago.
I was asked this in an interview. I couldn't answer. Not sure if it was a trick question.
let a = {}
and
let a = new Object

new Object()
has a default argument value of {}
You can also initialize an Array with new Object
const a = new Object([]);
See here.

Related

What does this constant definition mean [duplicate]

This question already has answers here:
What is destructuring assignment and its uses?
(3 answers)
Closed 8 months ago.
What does this assignment mean?
const { data: scoreData } = useMostRecentScore(studentId, loginId)
If useMostRecentScore returns an object, with a data property, it will create a new variable called scoreData and assign the contents of the data property.

why do I need to use the keyword new when using Date() in Javascript [duplicate]

This question already has answers here:
What is the 'new' keyword in JavaScript?
(17 answers)
Closed 2 years ago.
why do I need to use the keyword new when using Date().
var currentDate = new Date();
console.log(currentDate);
but if i just do the below I can still get the date.
console.log(Date());
The new keyword does the following things:
Creates a blank, plain JavaScript object
Links (sets the constructor of) this object to another object
Passes the newly created object from Step 1 as the this context
Returns this if the function doesn't return its own object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

What is the main difference between array and object in js [duplicate]

This question already has answers here:
What’s the difference between “{}” and “[]” while declaring a JavaScript array?
(8 answers)
Closed 4 years ago.
The point of confusion is - when we check the type of array or object using typeof get the return value as Object. So what's the main difference between them.
You can check using constructor.name
const myArr = []
const myObj = {}
console.log(`myArr is an ${myArr.constructor.name}`)
console.log(`my Obj is an ${myObj.constructor.name}`)

Convert static object to dynamic [duplicate]

This question already has answers here:
Adding Prototype to JavaScript Object Literal
(5 answers)
Closed 9 years ago.
If I have an object like this:
var obj = {};
I can't extend it because it hasn't got any prototype.
Is there any way to convert this object to dynamic so that it's possible to extend it and use new keyword. Something like:
obj.prototype.property = 'value';
var newobj = new obj;
That has nothing to do with static or dynamic.
You can only use the new operator on functions, not objects.
You cannot turn an object into a function; you need to create it as a function in the first place.
the only way is the following:
var obj = function () {};
because you can only use the new keyword with constructor function. That's it!

Constructor invocation without parentheses [duplicate]

This question already has answers here:
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 8 years ago.
Is there any difference between
var obj1 = new Constructor;
and
var obj2 = new Constructor();
given that Constructor is a constructor function?
According to the MDN docs:
[...] "new foo" is equivalent to "new foo()", i.e. if no argument list is
specified, "foo" is called without arguments.

Categories

Resources