JavaScript: `new RegExp('hi')` versus `RegExp('hi')`? [duplicate] - javascript

This question already has answers here:
JavaScript: using constructor without operator 'new'
(2 answers)
Closed 6 years ago.
What is the difference between RegExp('hi') and new RegExp('hi')?
Does the new keyword do anything here?

It is identical
The RegExp constructor is the %RegExp% intrinsic object and the initial value of the RegExp property of the global object. When RegExp is called as a function rather than as a constructor, it creates and initializes a new RegExp object. Thus the function call RegExp(…) is equivalent to the object creation expression new RegExp(…) with the same arguments.
From http://www.ecma-international.org/ecma-262/6.0/#sec-regexp-constructor

Related

Can new be applied to any function in JavaScript? [duplicate]

This question already has answers here:
How to check if a Javascript function is a constructor
(9 answers)
Closed 1 year ago.
Many sources like MDN and this tutorial define a concept called "constructor" that should be used with the new keyword to create an instance in traditional OOP's sense. But what "constructor" means is not formally stated.
It seems to me that literally any function can be used with new. (Though function without any manipulation to this in its definition is not particularly useful when newed, because it merely returns an empty object). Is this correct?
When you use the new keyword, it creates a new object. When you create an instance of a class with new, it creates an object, and any properties in the constructor method will be initialized in the object as well.

Simple Javascript OOP Confusion [duplicate]

This question already has answers here:
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 2 years ago.
To create a new empty object in javascript we can write in 2 ways.
1. With object literal syntax:
const obj = {}
2.Or with constructor function syntax:
const obj = new Object()
Today I accidentally typed out const obj = new Object saw it also worked, where it should've thrown
an error because I'd not invoked the constructor function which is done by a set of parenthesis.
I know that the new operator with a constructor function does 3 things.
1. creates a new empty object.
2. sets the value of this to the new object.
3. return the newly created object from the constructor function.
Is the constructor functions invocation is optional?
So what am I missing?
As a special case, for the new operator only, JavaScript simplifies the grammar by allowing the parenthesis to be omitted if there are no arguments in the function call. Here are some examples using the new operator:
o = new Object; // Optional parenthesis omitted here
d = new Date();
Originally answered here.

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.

“this” inside an Object pointing to an empty Object [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 5 years ago.
So I have this JavaScript code:
var obj={
a: “Chris”,
b: this
};console.log(obj.b); //{}
The console.log return an empty object rather than the object with variable a and b.
Anyone know why?
Thank you very much!
If this code is in the global scope, which I assume it is, this points to the global object (whatever it is). If you want it to point to the object itself, you cannot do it in the literal, you need to assign it later (after you create the object itself).

Javascript Functions and Objects Confusion [duplicate]

This question already has answers here:
Function and Object Javascript
(2 answers)
Closed 5 years ago.
In Javascript, we have two fundamental building blocks called functions and objects. But I'm a bit confused about the phrase functions are special type of objects. Anyways, in Javascript:
We create functions like this:
function foo(){}
Now the above declared function also behaves like an object as below:
foo.staticMethod = function(){}
Ok. I understand it.
Now similarly we create objects like this:
var obj = new Object() // Not using object literal here
That means, we need a function constructor Object to make even an empty object.
But Functions are also objects. How????
So my simple question is, if Object is used to create any new object, then how it can be an object itself as it accepts a property Object.prototype or I should say how a function can be an object ?
function Object(){
return {};
}

Categories