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

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.

Related

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.

If a string is not an object, how am I able to use built-in methods? [duplicate]

This question already has answers here:
How is a Javascript string not an object?
(2 answers)
Object and String prototypes are not prototypes of defined strings?
(3 answers)
Closed 4 years ago.
When you create an object in JavaScript, you inherit properties from the Object prototype you can utilize.
But a string is a primitive type, thus there is no prototype. So how am I able to use methods such as substr() and repeat() on a string when there is no prototype the string can inherit those methods from?
For example, when I create a new array and assign it to a variable, I type the variable name into the console and the Array prototype is listed where I have access to methods I can use. But if I create a string and assign the string to a variable, then I type the variable into the console, there is no prototype attached to it.
Does that make sense?
When you access a property on a primitive string, number, or other primitive, the JavaScript engine acts as though you've converted it to the equivalent object and then looks for the property. So for instance:
var str = "hi";
var upper = str.toUpperCase();
The JavaScript engine acts as though that code were written like this (for the purposes of accessing the toUpperCase property):
var str = "hi";
var upper = new String(str).toUpperCase();
Before ES5, the spec said that the JavaScript engine actually did create a string object there and then call the property. As of ES5 that changed slightly (because in strict mode it became possible for this to have a non-object value), but in essence the concept is still the same.

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

JS: how to define properties on numeric variables [duplicate]

This question already has answers here:
Extend primitives without prototyping them
(2 answers)
Closed 7 years ago.
I've few variables that are of type number (or for that matter strings too).
I want add some metadata to these variables.
When I try to use Object.defineProperty(myvar, "prop", {/*getter & setter*/}) it gives an error that myvar is not an object.
How can I define a property on non-objects with getter and setter methods?
I don't want to add anything to Number.prototype because I want these properties only on few variables.
You can do this by prototypal inheritance.
Using Number.prototype like this:
var proto = Object.create(Number.prototype);
function MyNumber() {
}
MyNumber.prototype = proto;
MyNumber.prototype.constructor = MyNumber;
MyNumber.prototype.someOwnMethod = function () {
}
// and so on
var myVar = new MyNumber();
Object.defineProperty(myvar, "prop", {/*getter & setter*/});
You'll get an Object in prototypes along with methods of Number.
Or if you do not need any own methods, just use Number constructor instead of number literal.
var myVar = new Number();
Object.defineProperty(myvar, "prop", {/*getter & setter*/});
The same for strings:
var myVar = new String();

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