Simple Javascript OOP Confusion [duplicate] - javascript

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.

Related

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 {};
}

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

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

JavaScript: Why Declare Object with Constructor instead of Blank Literal? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
creating objects - new object or object literal notation?
Literal notation VS. constructor to create objects in JavaScript
I'm going through my very first Javascript tutorial.
I just found two ways to create a JS object.
var person = new Object();
person.name = "Tom";
person.age = "17";
and
var person = {};
person.name = "Tom";
person.name = "17"
Any difference between these two ways of object creation? Since the second looks simpler, can we always use it under any condition?
Not only is the second syntax easier to read and not only will it work under any condition, but the first syntax might not work under all conditions:
function Object() {
// Oh crap, we have redefined Object!
return []; // return an array because we are EVIL
}
var person = new Object(); // not what we think it is
But {}, being a syntactic construct, is immune to such evil trickery.
In addition, the object literal notation can be partially optimized at parse time, since after all there's only one object type that could be created. That may result in a minuscule performance increase.

What is return new function(); in JavaScript? [duplicate]

This question already has answers here:
What is the 'new' keyword in JavaScript?
(17 answers)
Closed 8 years ago.
In js code I have seen this used:
function doStuff( selector ) {
/* Stuff to do with selector */
}
var q = function( selector ) {
return new doStuff( selector );
}
What exactly is happening? What is return new really doing? It seems to pass its arguments to the other function, but would someone please be kind enough to walk me through the process?
All and any help is appreciated, thanks in advance.
When we call a function with the new keyword. the following will happen:
A new object will be created in the memory
The scope of that object will be passed to the function; So the this keyword will refer to that object.
The newly created object will be returned.
So in essence, that is how you create instances in JavaScript. You need to call a function with the new keyword. When doing so, the function is called constructor.
In your example, the q function returns an instance of the doStuff method. Bare in mind though that the naming convention is not correct.
Constructors should be nouns rather that verbs and they should be in Pascal-case, not camel-case

Javascript object constructor vs object literal [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
creating objects - new object or object literal notation?
Literal notation VS. constructor to create objects in JavaScript
I'm going through my very first Javascript tutorial.
I just found two ways to create a JS object.
var person = new Object();
person.name = "Tom";
person.age = "17";
and
var person = {};
person.name = "Tom";
person.name = "17"
Any difference between these two ways of object creation? Since the second looks simpler, can we always use it under any condition?
Not only is the second syntax easier to read and not only will it work under any condition, but the first syntax might not work under all conditions:
function Object() {
// Oh crap, we have redefined Object!
return []; // return an array because we are EVIL
}
var person = new Object(); // not what we think it is
But {}, being a syntactic construct, is immune to such evil trickery.
In addition, the object literal notation can be partially optimized at parse time, since after all there's only one object type that could be created. That may result in a minuscule performance increase.

Categories