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.
Related
This question already has answers here:
Constructor behaving differently using ES6 shorthand notation
(3 answers)
TypeError: function is not a constructor (evaluating 'new self.f(1)')
(1 answer)
What is the difference between these object literals?
(1 answer)
Closed 5 months ago.
Regardless of intent, it's unclear to me why one is valid and other other is not. If someone could point to the relevant part of the language spec, that would be especially helpful.
class Foo {
bar() {}
}
Foo.prototype.baz = function() {};
const f = new Foo();
new f.baz(); // Valid
new f.bar(); // invalid, throws error
As pointed out in the comments on the original question, the spec defines it this way. A constructor must have the internal [[Construct]]s, and while ordinary function expressions do get this slot set, method definitions do not. This is described in MDN (as another commenter pointed out) as well.
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.
This question already has answers here:
Why can't I set the 'prototype' of a function created using 'bind'?
(4 answers)
Closed 2 years ago.
let f = Array.bind({a:1})
f.prototype === undefined // true
typeof f === 'function' // true
Almost all of JavaScript's functions have a prototype property. I'm wondering what happens in bind. Did it remove prototype?
It's in the official JavaScript spec, ECMAScript 2020:
NOTE 1
Function objects created using Function.prototype.bind are exotic
objects. They also do not have a prototype property.
These so-called "bound function exotic objects" are not regular Function objects.
This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
No colon after property name in object declaration, is it valid? [duplicate]
(2 answers)
Closed 2 years ago.
I'm reading about mixins here and it uses this valid code:
let sayHiMixin = {
sayHi() {
alert(`Hello ${this.name}`);
},
sayBye() {
alert(`Bye ${this.name}`);
}
};
console.log(sayHiMixin);
sayHiMixin is a valid object. Can anyone point out to me how the syntax for writing this object is valid (maybe an MDN page)? I've never seen it before except for when writing methods in a class.
This question already has answers here:
Constructor invocation without parentheses [duplicate]
(1 answer)
What is the difference between new Foo and new Foo() in javascript?
(3 answers)
Invoking a function without parentheses
(9 answers)
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 5 years ago.
var foo= function(){
this.a=1;
}
var obj= new foo;
console.log(obj);
how can object be created using foo instead of foo() ?
will the constructor will be called if i don't use parentheses ?
i think foo is also function in order to call the inside code we need parentheses but here why it doesn't need ?