[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Calling constructor function with or without new

Steven D'Aprano

6/7/2015 2:08:00 PM

I'm using Rhino. What's the difference between calling a constructor with or
without new?

js> var x = Number("123");
js> print(x);
123
js> typeof x;
number


js> var y = new Number("123");
js> typeof y;
object


Why is x a "number" rather than a Number? Why is y an "object"?



--
Steven

19 Answers

ram

6/7/2015 4:56:00 PM

0

Steven D'Aprano <steve@pearwood.info> writes:
>What's the difference between calling a constructor with or
>without new?

With new, in the constructor, »this« gets assigned to a new
object that has the function as its constructor and the
function's prototype as its internal prototype.

A function can detect whether it was possibly invoked as
a constructor.

For example,

function Alpha( arg )
{ "use strict"; if( !( this instanceof Alpha )) ... else ... }

.

One can use this to align the function's behavior with the
constructor:

function Alpha( arg )
{ "use strict";
if( !( this instanceof Alpha ))return new Alpha( arg ); else ... }

In the standard library, several constructors can also be
called as functions, albeit sometimes with different semantics.

In strict mode, »this« is not defined in function calls,
so erroneously forgetting »new« will be detected more often.

Martin Honnen

6/7/2015 5:47:00 PM

0

Steven D'Aprano wrote:
> I'm using Rhino. What's the difference between calling a constructor with or
> without new?
>
> js> var x = Number("123");
> js> print(x);
> 123
> js> typeof x;
> number

As you use Rhino which I think did never get updated to implement
ECMAScript 5 you might want to download
http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%...
and read section "15.7.1 The Number Constructor Called as a Function"
which says "When Number is called as a function rather than as a
constructor, it performs a type conversion." and defines
Number ( [ value ] )
as
Returns a number value (not a Number object) computed by
ToNumber(value) if value was supplied,
else returns +0.

The type conversions "ToNumber" performs depending on the type of the
value are defined in section "9.3" of that PDF.






--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

JR

6/8/2015 12:51:00 AM

0

On 07/06/15 14:46, Martin Honnen wrote:
> Steven D'Aprano wrote:
>> I'm using Rhino. What's the difference between calling a constructor
>> with or
>> without new?
>>
>> js> var x = Number("123");
>> js> print(x);
>> 123
>> js> typeof x;
>> number
>
> As you use Rhino which I think did never get updated to implement
> ECMAScript 5 you might want to download
> http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%...
> and read section "15.7.1 The Number Constructor Called as a Function"
> which says "When Number is called as a function rather than as a
> constructor, it performs a type conversion." and defines
> Number ( [ value ] )
> as
> Returns a number value (not a Number object) computed by
> ToNumber(value) if value was supplied,
> else returns +0.
>
> The type conversions "ToNumber" performs depending on the type of the
> value are defined in section "9.3" of that PDF.
>

One can read the online version at:
<http://ecma-international.org/ecma-262/5.1/#sec-...
<http://ecma-international.org/ecma-262/5.1/#s...


--
Joao Rodrigues

Thomas 'PointedEars' Lahn

6/9/2015 1:06:00 PM

0

Joao Rodrigues wrote:

> On 07/06/15 14:46, Martin Honnen wrote:
>> Steven D'Aprano wrote:
>> As you use Rhino which I think did never get updated to implement
>> ECMAScript 5 you might want to download
>> http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%...
>> [â?¦]
>
> One can read the online version at:
> <http://ecma-international.org/ecma-262/5.1/#sec-...
> <http://ecma-international.org/ecma-262/5.1/#s...

No, those are the *non-normative* versions of the *5.1* Edition.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

JR

6/9/2015 3:46:00 PM

0

On 09/06/2015 10:05, Thomas 'PointedEars' Lahn wrote:
> Joao Rodrigues wrote:
>
>> On 07/06/15 14:46, Martin Honnen wrote:
>>> Steven D'Aprano wrote:
>>> As you use Rhino which I think did never get updated to implement
>>> ECMAScript 5 you might want to download
>>> http:/...international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%...
>>> [â?¦]
>>
>> One can read the online version at:
>> <http://ecma-international.org/ecma-262/5.1/#sec-...
>> <http://ecma-international.org/ecma-262/5.1/#s...
>
> No, those are the *non-normative* versions of the *5.1* Edition.
>

ACK the wrong edition (5.1 instead of the 3rd edition), but not the
"non-normative". One can read the preamble of the HTML version [1] which
states the following:

| This is the HTML rendering of Ecma-262 Edition 5.1, The ECMAScript
| Language Specification.
|
| The PDF rendering of this document is located at http:/...
| international.org/ecma-262/5.1/ECMA-262.pdf.
|
| The PDF version is the definitive specification. Any discrepancies
| between this HTML version and the PDF version are unintentional.

[1] <http:/...international.org/ecma-262/5.1/>
--
Joao Rodrigues

Thomas 'PointedEars' Lahn

6/9/2015 4:32:00 PM

0

Joao Rodrigues wrote:

> On 09/06/2015 10:05, Thomas 'PointedEars' Lahn wrote:
>> Joao Rodrigues wrote:
>>> On 07/06/15 14:46, Martin Honnen wrote:
>>>> Steven D'Aprano wrote:
>>>> As you use Rhino which I think did never get updated to implement
>>>> ECMAScript 5 you might want to download
>>>> http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%...
>>>> [â?¦]
>>>
>>> One can read the online version at:
>>> <http://ecma-international.org/ecma-262/5.1/#sec-...
>>> <http://ecma-international.org/ecma-262/5.1/#s...
>>
>> No, those are the *non-normative* versions of the *5.1* Edition.
>
> ACK the wrong edition (5.1 instead of the 3rd edition), but not the
> "non-normative".

Wrong.

> One can read the preamble of the HTML version [1]

Why donâ??t you?

> which states the following:
>
> | This is the HTML rendering of Ecma-262 Edition 5.1, The ECMAScript
> | Language Specification.
> |
> | The PDF rendering of this document is located at http://www... | international.org/ecma-262/5.1/ECMA-262.pdf.
> |
> | The PDF version is the definitive specification. Any discrepancies
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> | between this HTML version and the PDF version are unintentional.

AISB, the HTML version is _not_ the normative (or, as Jason Orendorff put it
there, the "definitive") one. It never was. The PDF versions are those
that the Ecma International General Assembly voted on, the *normative* ones.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

JR

6/9/2015 5:52:00 PM

0

Thomas Lahn wrote:
> Joao Rodrigues wrote:
>
>> On 09/06/2015 10:05, Thomas 'PointedEars' Lahn wrote:
>>> Joao Rodrigues wrote:
>>>> On 07/06/15 14:46, Martin Honnen wrote:
>>>>> Steven D'Aprano wrote:
>>>>> As you use Rhino which I think did never get updated to implement
>>>>> ECMAScript 5 you might want to download
>>>>> http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%...
>>>>> [â?¦]
>>>>
>>>> One can read the online version at:
>>>> <http://ecma-international.org/ecma-262/5.1/#sec-...
>>>> <http://ecma-international.org/ecma-262/5.1/#s...
>>>
>>> No, those are the *non-normative* versions of the *5.1* Edition.
>>
>> ACK the wrong edition (5.1 instead of the 3rd edition), but not the
>> "non-normative".
>
> Wrong.
>


>
>> which states the following:
>>
>> | This is the HTML rendering of Ecma-262 Edition 5.1, The ECMAScript
>> | Language Specification.
>> |
>> | The PDF rendering of this document is located at http://www... | international.org/ecma-262/5.1/ECMA-262.pdf.
>> |
>> | The PDF version is the definitive specification. Any discrepancies
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> | between this HTML version and the PDF version are unintentional.
>
> AISB, the HTML version is _not_ the normative (or, as Jason Orendorff put it
> there, the "definitive") one. It never was. The PDF versions are those
> that the Ecma International General Assembly voted on, the *normative* ones.
>

Ecma does not affirm there is a normative or a non-normative version of
the specification. That confusion lies only within your delusional mind.

The preamble mentioned contains a legal disclaimer about any
unintentional discrepancies between the two versions:

| Any discrepancies between this HTML version and the PDF version
| are unintentional.

--
Joao Rodrigues

Thomas 'PointedEars' Lahn

6/9/2015 8:39:00 PM

0

Joao Rodrigues wrote:

> Thomas Lahn wrote:
>> Joao Rodrigues wrote:
>>> | This is the HTML rendering of Ecma-262 Edition 5.1, The ECMAScript
>>> | Language Specification.
>>> |
>>> | The PDF rendering of this document is located at http://www... |
>>> | international.org/ecma-262/5.1/ECMA-262.pdf.
>>> |
>>> | The PDF version is the definitive specification. Any discrepancies
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> | between this HTML version and the PDF version are unintentional.
>>
>> AISB, the HTML version is _not_ the normative (or, as Jason Orendorff put
>> it there, the "definitive") one. It never was. The PDF versions are
>> those that the Ecma International General Assembly voted on, the
>> *normative* ones.
>
> Ecma does not affirm there is a normative or a non-normative version of
> the specification.

Yes, they do, in the very source that you are citing and quoting from.

> That confusion lies only within your delusional mind.

/Ad hominem/ when you are out of good arguments. I see.

> The preamble mentioned contains a legal disclaimer about any
> unintentional discrepancies between the two versions:
>
> | Any discrepancies between this HTML version and the PDF version
> | are unintentional.

However, if there are discrepancies, then the PDF version is the one that
matters, the â??definitiveâ? or the â??normativeâ? one. Because it is the one the
Ecma membership has seen and voted on (they voted on a final draft that
would undergo changes editorial in nature at most, as from Edition 5 to the
5.1 Edition), that they thereby declared an industry standard or *norm*.

If you do not understand the standardization processes of the computer
industry, and you really do not, I strongly suggest you get yourself
informed about it on the respective Web sites, and do not post on the
subject again until you did that.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

JR

6/9/2015 10:37:00 PM

0

Thomas Lahn wrote:
> Joao Rodrigues wrote:
>
>> Thomas Lahn wrote:
>>> Joao Rodrigues wrote:
> >>> | This is the HTML rendering of Ecma-262 Edition 5.1, The ECMAScript
>>>> | Language Specification.
>>>> |
>>>> | The PDF rendering of this document is located at http://www... |
>>>> | international.org/ecma-262/5.1/ECMA-262.pdf.
>>>> |
>>>> | The PDF version is the definitive specification. Any discrepancies
>>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> | between this HTML version and the PDF version are unintentional.
>>>
>>> AISB, the HTML version is _not_ the normative (or, as Jason Orendorff put
>>> it there, the "definitive") one. It never was. The PDF versions are
>>> those that the Ecma International General Assembly voted on, the
>>> *normative* ones.
>>
>> Ecma does not affirm there is a normative or a non-normative version of
>> the specification.
>
> Yes, they do, in the very source that you are citing and quoting from.

Why Ecma would need to affirm ECMA-262 whatever edition is a normative
specification? Of course it is, unless stated contrarily.

>
>> That confusion lies only within your delusional mind.
>
> /Ad hominem/ when you are out of good arguments. I see.

You do the same all the time - only *you* do not realize this *fact*. So
I'm just acting like a mirror.

>
>> The preamble mentioned contains a legal disclaimer about any
>> unintentional discrepancies between the two versions:
>>
>> | Any discrepancies between this HTML version and the PDF version
>> | are unintentional.
>
> However, if there are discrepancies, then the PDF version is the one that
> matters, the â??definitiveâ? or the â??normativeâ? one.

Firstly you dismissed the word 'definitive', by writing 'it never
was'... Now you are introducing a new meaning for it as 'normative'
which does not appear anywhere on the Ecma website or the preamble of
the specification.

So I have to *repeat* the citation in my first reply to your post (the
one you have highlighted on your reply):

| The PDF version is the *definitive* specification. Any discrepancies
| between this HTML version and the PDF version are unintentional.

It means that the PDF version will prevail over the HTML version in case
of unintentional discrepancies found by chance between them. It does not
mean that the PDF version is "normative" or the HTML version is
"non-normative" as you are insisting to blare out. Come off it!

> If you do not understand the standardization processes of the computer
> industry, and you really do not, I strongly suggest you get yourself
> informed about it on the respective Web sites, and do not post on the
> subject again until you did that.
>

/Ad hominem/ when you are out of good arguments. See?

--
Joao Rodrigues

Thomas 'PointedEars' Lahn

6/10/2015 4:15:00 PM

0

Joao Rodrigues wrote:

> Thomas Lahn wrote:
>> Joao Rodrigues wrote:
>>> Thomas Lahn wrote:
>>>> Joao Rodrigues wrote:
>>>>> | This is the HTML rendering of Ecma-262 Edition 5.1, The ECMAScript
>>>>> | Language Specification.
>>>>> |
>>>>> | The PDF rendering of this document is located at http://www... |
>>>>> | international.org/ecma-262/5.1/ECMA-262.pdf.
>>>>> |
>>>>> | The PDF version is the definitive specification. Any discrepancies
>>>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>>> | between this HTML version and the PDF version are unintentional.
>>>>
>>>> AISB, the HTML version is _not_ the normative (or, as Jason Orendorff
>>>> put
>>>> it there, the "definitive") one. It never was. The PDF versions are
>>>> those that the Ecma International General Assembly voted on, the
>>>> *normative* ones.
>>> Ecma does not affirm there is a normative or a non-normative version of
>>> the specification.
>> Yes, they do, in the very source that you are citing and quoting from.
>
> Why Ecma would need to affirm ECMA-262 whatever edition is a normative
> specification? Of course it is, unless stated contrarily.

No, if there are several versions of a technical specification, then it
needs to be specified which of them are the normative ones. In this case,
there is a PDF version and an HTML version (formerly, there have been PDF
and Word document versions).

AISB, the people responsible for making the Specification an industry
standard, the members that participate in the Ecma International General
Assembly, have not seen the HTML version and they have not voted on it
because it was produced afterwards. So it cannot be the normative one.

It follows, and has been stated by the author of the HTML version, that the
definitive one is the PDF version (meaning that where the HTML version
differs from the PDF version, the PDF version matters), which makes it also
the normative one.

> [nonsense]

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.