This question already has answers here:
Array() vs new Array()
(4 answers)
Closed 3 years ago.
Why are these two statements the same in JavaScript?
var a = new Array(5);
var a = Array(5);
Or, if they are not the same, what is different about them? Basically if I assign a to a different variable and mutate it, the values in the array change for each initialization in a similar way.
Why are these two statements the same in JavaScript?
var a = new Array(5);
var a = Array(5);
That's just the way Array was designed. Not all constructors have this behavior, but the Array constructor's specification is such that it can be called either with new or without.
From the ECMAScript spec:
22.1.1The Array Constructor
[...]
also creates and initializes a new Array object when called as a function rather than as a constructor. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.
Related
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
This question already has answers here:
javascript pass by reference workaround
(2 answers)
Closed 6 years ago.
So i have an javascript object called gamewhich has two methods:
function game() {
this.TxtRevealed = new Array();
this.TxtRevealedBackup = new Array();
[...]
}
Now, outside an object i assign one two another:
game.TxtRevealedBackup = game.TxtRevealed;
After a while i change game.TxtRevealed(i use slice function to cut some values from it).
And now happens something i do not intend: automatically game.TxtRevealedBackup changes also to new value of game.TxtRevealed.
I'd expect that game.TxtRevealedBackup would be same as game.TxtRevealed was in moment of assigning. It works as if game.TxtRevealedBackup was pointing to value that is represented by game.TxtRevealed continously, not the value it was in moment of assignment.
Why is it happening and how to make it working i'd expect?
Kalreg.
Objects do work by reference. If you want to clone objects see this answer.
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!
This question already has answers here:
Can we omit parentheses when creating an object using the "new" operator?
(6 answers)
Closed 5 years ago.
Think about the silution
function F(){}; //This is a Constructor function
Who can tell me there is any different between
var myInstance = new F;
and
var myInstance = new F();
? The new keyword execute followed Function immediately anyway whatever that is following by partheses ?
There is no difference. From the Mozilla Docs:
new constructor[([arguments])]
The parentheses are in square brackets, that means they are optional.
There is no practical difference, omitting the paranthesis can be done if no arguments are passed to simplify the grammar.
Note that some validators such as JSLint will report a warning if you leave them out though, as it is considered more consistent to always use the same syntax for invoking constructor functions regardless of arguments.
This similar example would be very bad if you get into this lazy habit:
var one = myFunc;
var two = myFunc();
These are two different variables, one is a function reference and the other is the return value of the function.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the difference between Array(1) and new Array(1) in JavaScript?
In javascript, how are these two different?
var arr = Array();
var arr2 = new Array();
If they are the same according to JS standards, are there any browsers that treat both ways differently?
According to ECMA-262 edition 5.1:
15.4.1 The Array Constructor Called as a Function
When Array is called as a function rather than as a constructor,
it creates and initialises a new Array object.
Thus the function call Array(...) is equivalent to the object creation
expression new Array(...) with the same arguments.
The section 11.1.4 (it is quite long, so I won't quote it) also states that array literal directly corresponds to the new Array(...) constructor call.
I believe the 2nd is simply proper coding convention, but however Jared has a good point that most people just use var arr = [];
Here's a benchmark for your question: http://jsperf.com/array-vs-new-array
After 10 runs, they're neck and neck averaging 65-80 millions ops per second. I see no performance difference whatsoever between the two.
That being said, I added var arr = []; to the benchmark, and it is consistently 20-30% faster than the other two, clocking in at well over 120 million ops per second.