[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Can somebody explain this to me

Tony Johansson

1/5/2015 11:49:00 PM

The code at the end works but I have a question.
When I have this code construction with prototype I can call the getFoo in
this way var poo = f.getFoo(); // "bar"
P.prototype.getFoo = function ()
{
return this.foo;
};


I mean if I add this piece of code into the class definition I can call the
getFoo in this way var poo = f.getFoo(); // "bar"

this.getFoo = function ()
{
return this.foo;
};

So as a summary can somebody explain what is the advantage to use a
prototype in this exemple?


Here is the complete code
********************
<script type="text/javascript">

function P(foo) //Here is the class definition
{
this.foo = foo;
// implicit return av "this" i den fall då P anropas som
konstruktor

}

P.prototype.getFoo = function ()
{
return this.foo;
};

var f = new P("bar");
var g = new P("baz");

var poo = f.getFoo(); // "bar"
g.getFoo(); // "baz"
</script>

69 Answers

Thomas 'PointedEars' Lahn

1/6/2015 12:40:00 AM

0

Tony Johansson wrote:

> The code at the end works but I have a question.
> When I have this code construction with prototype I can call the getFoo in
> this way var poo = f.getFoo(); // "bar"
> P.prototype.getFoo = function ()
> {

I recommend to put the opening brace of a function expression on the same
line as the â??functionâ? keyword in order to tell function declarations and
function expressions apart more easily.

> return this.foo;
> };
>
>
> I mean if I add this piece of code into the class definition

There are no classes in this case; prototype-based inheritance is used.

> I can call the getFoo in this way var poo = f.getFoo(); // "bar"
^ method

> this.getFoo = function ()
> {
> return this.foo;
> };
>
> So as a summary can somebody explain what is the advantage to use a
> prototype in this exemple?

Inherited methods can be reused. In this example, one advantage is that
only one Function instance is created, which saves memory as compared to the
instance method approach. It also saves runtime because the creation of
that object only happens once. (There are also disadvantages.)

> function P(foo) //Here is the class definition

No, there is the declaration of a function named â??Pâ? that can be called as a
constructor. This also means that a Function instance (an object) is being
created that can be referred to by â??Pâ?.

> {
> this.foo = foo;
> // implicit return av "this" i den fall då P anropas som
> konstruktor

Correct.

> var f = new P("bar");

Using the â??newâ? keyword on a reference to an object that implements the
internal [[Construct]] method (which applies to Function instances) calls
that object as a constructor.

--
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.

John Harris

1/6/2015 4:02:00 PM

0

On Tue, 06 Jan 2015 01:40:16 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Tony Johansson wrote:
>
>> The code at the end works but I have a question.
>> When I have this code construction with prototype I can call the getFoo in
>> this way var poo = f.getFoo(); // "bar"
>> P.prototype.getFoo = function ()
>> {
>
>I recommend to put the opening brace of a function expression on the same
>line as the ?function? keyword in order to tell function declarations and
>function expressions apart more easily.

And I recommend that you don't change, though a smaller indent
wouldn't hurt. Having the curly brackets lined up makes it easier to
see where the code body begins and ends, especially when the body
contains inner function definitions.


>> return this.foo;
>> };
>>
>>
>> I mean if I add this piece of code into the class definition
>
>There are no classes in this case;

Not true. See an earlier thread.


>prototype-based inheritance is used.

>> I can call the getFoo in this way var poo = f.getFoo(); // "bar"
> ^ method
>
>> this.getFoo = function ()
>> {
>> return this.foo;
>> };
>>
>> So as a summary can somebody explain what is the advantage to use a
>> prototype in this exemple?
>
>Inherited methods can be reused. In this example, one advantage is that
>only one Function instance is created, which saves memory as compared to the
>instance method approach. It also saves runtime because the creation of
>that object only happens once. (There are also disadvantages.)
>
>> function P(foo) //Here is the class definition
<snip>

No; here is the class's constructor function. The class's definition
has to be deduced from the code, which is presumably why some people
say ECMAScript, up to v5 at least, has no classes.


John

Thomas 'PointedEars' Lahn

1/6/2015 4:26:00 PM

0

John Harris wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Tony Johansson wrote:
>>> The code at the end works but I have a question.
>>> When I have this code construction with prototype I can call the getFoo
>>> in
>>> this way var poo = f.getFoo(); // "bar"
>>> P.prototype.getFoo = function ()
>>> {
>>
>>I recommend to put the opening brace of a function expression on the same
>>line as the Â?functionÂ? keyword in order to tell function declarations and
>>function expressions apart more easily.
>
> And I recommend that you don't change, though a smaller indent
> wouldn't hurt. Having the curly brackets lined up makes it easier to
> see where the code body begins and ends, especially when the body
> contains inner function definitions.

However, as you have provably little if any theoretical knowledge of and
practical experience with the programming languages in question, your
recommendations should be weighed against that.

There is an important difference between function declarations and function
expressions that is _not_ syntactical, and it is wise to write source code
so that such differences are easier to see.

>>> return this.foo;
>>> };
>>>
>>>
>>> I mean if I add this piece of code into the class definition
>>
>>There are no classes in this case;
>
> Not true. See an earlier thread.

In the earlier thread you already failed to provide convincing, let alone
non-fallacious arguments that the terminology you prefer, in the way you
prefer it, is useful in a technical discussion on the subjects that are on-
topic here.

>>prototype-based inheritance is used.
>
>>> I can call the getFoo in this way var poo = f.getFoo(); // "bar"
>> ^ method
>>
>>> this.getFoo = function ()
>>> {
>>> return this.foo;
>>> };
>>>
>>> So as a summary can somebody explain what is the advantage to use a
>>> prototype in this exemple?
>>
>>Inherited methods can be reused. In this example, one advantage is that
>>only one Function instance is created, which saves memory as compared to
>>the instance method approach. It also saves runtime because the creation
>>of that object only happens once. (There are also disadvantages.)
>>
>>> function P(foo) //Here is the class definition
> <snip>
>
> No; here is the class's constructor function.

Utter nonsense.

> The class's definition has to be deduced from the code,

There is no class as there are no classes in the respective original
language (what is created from that by compilers for virtual machines for
optimization, indeed, is irrelevant in that regard).

Therefore there is no â??class definitionâ? to â??deduceâ? other than that which
can only be ascribed to either too vivid imagination or utter wannabe-ness.
The constructor functionâ??s declaration, on the other hand, is plain to see,
it can be looked up in authoritative reference material, and it is what this
code is being parsed as by *all* relevant script engines.

> which is presumably why some people say ECMAScript, up to v5 at least, has
> no classes.

ECMAScript Editions 1 to 3, and 5.x, simply do not; neither have their
implementations. (The abandoned Edition 4 working draft is irrelevant with
regard to client-side code in Web browsers, but it must be excluded from
that list.)

However, ECMAScript Edition 6 and its implementations are going to, and they
are going to have prototype-based inheritance as well. Therefore it is not
useful to call something a class that is not called a class in any
authoritative reference material. Please stop confusing people who seek
clarity here.

--
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.

Tim Streater

1/6/2015 5:34:00 PM

0

In article <qj1oaahmnp3r6j45b27sm5ujsfhqu4igv6@4ax.com>, John Harris
<niam@jghnorth.org.uk.invalid> wrote:

>On Tue, 06 Jan 2015 01:40:16 +0100, Thomas 'PointedEars' Lahn
><PointedEars@web.de> wrote:
>
>>Tony Johansson wrote:
>>
>>> The code at the end works but I have a question.
>>> When I have this code construction with prototype I can call the getFoo in
>>> this way var poo = f.getFoo(); // "bar"
>>> P.prototype.getFoo = function ()
>>> {
>>
>>I recommend to put the opening brace of a function expression on the same
>>line as the â??functionâ?° keyword in order to tell function declarations and
>>function expressions apart more easily.
>
>And I recommend that you don't change, though a smaller indent
>wouldn't hurt. Having the curly brackets lined up makes it easier to
>see where the code body begins and ends, especially when the body
>contains inner function definitions.

Agree 100%. I see that PointyHead has given his opinion is his usual
sneering way; he may safely be ignored.

--
"... you must remember that if you're trying to propagate a creed of
poverty, gentleness and tolerance, you need a very rich, powerful,
authoritarian organisation to do it." - Vice-Pope Eric

Christoph M. Becker

1/6/2015 6:04:00 PM

0

Tim Streater wrote:

> In article <qj1oaahmnp3r6j45b27sm5ujsfhqu4igv6@4ax.com>, John Harris
> <niam@jghnorth.org.uk.invalid> wrote:
>
>> On Tue, 06 Jan 2015 01:40:16 +0100, Thomas 'PointedEars' Lahn
>> <PointedEars@web.de> wrote:
>>
>>> Tony Johansson wrote:
>>>
>>>> The code at the end works but I have a question.
>>>> When I have this code construction with prototype I can call the
>>>> getFoo in
>>>> this way var poo = f.getFoo(); // "bar"
>>>> P.prototype.getFoo = function ()
>>>> {
>>>
>>> I recommend to put the opening brace of a function expression on the
>>> same line as the â??functionâ?° keyword in order to tell function
>>> declarations and function expressions apart more easily.
>>
>> And I recommend that you don't change, though a smaller indent
>> wouldn't hurt. Having the curly brackets lined up makes it easier to
>> see where the code body begins and ends, especially when the body
>> contains inner function definitions.
>
> Agree 100%. I see that PointyHead has given his opinion is his usual
> sneering way; he may safely be ignored.

Thomas '_Pointed Ears_', being aware of the difference between a
function declaration and a function expression, advanced a *sound*
*argument* -- I don't see how this can be reasonably called "in a
sneering way". BTW: because of this difference, some programmers even
avoid function declarations at all.

John's argument might be regarded secondary (if not invalid) nowadays.

--
Christoph M. Becker

Joao Rodrigues

1/7/2015 2:46:00 AM

0

On 01/06/2015 02:26 PM, Thomas 'PointedEars' Lahn wrote:
> John Harris wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> Tony Johansson wrote:

<snip>

>>>> return this.foo;
>>>> };
>>>>
>>>>
>>>> I mean if I add this piece of code into the class definition
>>>
>>> There are no classes in this case;
>>
>> Not true. See an earlier thread.
>
> In the earlier thread you already failed to provide convincing, let alone
> non-fallacious arguments that the terminology you prefer, in the way you
> prefer it, is useful in a technical discussion on the subjects that are on-
> topic here.
>
>>> prototype-based inheritance is used.
>>
>>>> I can call the getFoo in this way var poo = f.getFoo(); // "bar"
>>> ^ method
>>>
>>>> this.getFoo = function ()
>>>> {
>>>> return this.foo;
>>>> };
>>>>
>>>> So as a summary can somebody explain what is the advantage to use a
>>>> prototype in this exemple?
>>>
>>> Inherited methods can be reused. In this example, one advantage is that
>>> only one Function instance is created, which saves memory as compared to
>>> the instance method approach. It also saves runtime because the creation
>>> of that object only happens once. (There are also disadvantages.)
>>>
>>>> function P(foo) //Here is the class definition
>> <snip>
>>
>> No; here is the class's constructor function.
>
> Utter nonsense.
>
>> The class's definition has to be deduced from the code,
>
> There is no class as there are no classes in the respective original
> language (what is created from that by compilers for virtual machines for
> optimization, indeed, is irrelevant in that regard).
>
> Therefore there is no â??class definitionâ? to â??deduceâ? other than that which
> can only be ascribed to either too vivid imagination or utter wannabe-ness.
> The constructor functionâ??s declaration, on the other hand, is plain to see,
> it can be looked up in authoritative reference material, and it is what this
> code is being parsed as by *all* relevant script engines.
>
>> which is presumably why some people say ECMAScript, up to v5 at least, has
>> no classes.
>
> ECMAScript Editions 1 to 3, and 5.x, simply do not; neither have their
> implementations. (The abandoned Edition 4 working draft is irrelevant with
> regard to client-side code in Web browsers, but it must be excluded from
> that list.)

In _Effective JavaScript_, David Herman, a member of the Ecma TC39, the
committee responsible for the standardization of JavaScript, wrote:

"4. Objects and Prototypes

Objects are JavaScriptâ??s fundamental data structure. Intuitively, an
object represents a table relating strings to values. But when you dig
deeper, there is a fair amount of machinery that goes into objects.
Like many object-oriented languages, JavaScript provides support for
implementation inheritance: the reuse of code or data through a dynamic
delegation mechanism. _But unlike many conventional languages,
JavaScriptâ??s inheritance mechanism is based on prototypes rather than
classes. For many programmers, JavaScript is the first object-oriented
language they encounter without classes._

In many languages, every object is an instance of an associated class,
which provides code shared between all its instances. JavaScript, by
contrast, has no built-in notion of classes. Instead, objects inherit
from other objects. Every object is associated with some other object,
known as its prototype. Working with prototypes can be different from
classes, although many concepts from traditional object-oriented
languages still carry over."

>
> However, ECMAScript Edition 6 and its implementations are going to, and they
> are going to have prototype-based inheritance as well. Therefore it is not
> useful to call something a class that is not called a class in any
> authoritative reference material. Please stop confusing people who seek
> clarity here.
>

ACK.

--
Joao Rodrigues

Thomas 'PointedEars' Lahn

1/7/2015 3:36:00 AM

0

Joao Rodrigues wrote:

> In _Effective JavaScript_, David Herman, a member of the Ecma TC39, the
> committee responsible for the standardization of JavaScript, wrote: [â?¦]

The Ecma Technical Committee (TC) 39 is _not_ â??responsible for the
standardization of JavaScript�; it never has been (the first Edition of
ECMAScript already reconciled differences between Netscape JavaScript and
Microsoft *JScript*). They are responsible for publishing working drafts of
Editions of the ECMAScript Language Specification and related specifications
for the Ecma (International) General Assembly to vote on, so that they can
become an Ecma Standard.

The programming languages that carry the â??JavaScriptâ? name, AFAIK most of
them without due licensing, are not the only (conforming) implementations of
the Editions of ECMAScript.

ACK to the rest.

--
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.

John Harris

1/7/2015 3:08:00 PM

0

On Tue, 06 Jan 2015 19:03:33 +0100, "Christoph M. Becker"
<cmbecker69@arcor.de> wrote:


<snip>
>Thomas '_Pointed Ears_', being aware of the difference between a
>function declaration and a function expression, advanced a *sound*
>*argument* -- I don't see how this can be reasonably called "in a
>sneering way". BTW: because of this difference, some programmers even
>avoid function declarations at all.

Someone who knows a little bit about ECMAScript would know that
" = function "
is absolutely certain to indicate a function expression. A short
comment could be added if thought to be useful.


>John's argument might be regarded secondary (if not invalid) nowadays.

It's still primary. Seeing where function definitions start and
finish, quickly, is vital to maintenance programmers.

John

John Harris

1/7/2015 3:38:00 PM

0

On Tue, 06 Jan 2015 17:26:24 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>John Harris wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> Tony Johansson wrote:
>>>> The code at the end works but I have a question.
>>>> When I have this code construction with prototype I can call the getFoo
>>>> in
>>>> this way var poo = f.getFoo(); // "bar"
>>>> P.prototype.getFoo = function ()
>>>> {
>>>
>>>I recommend to put the opening brace of a function expression on the same
>>>line as the ?function? keyword in order to tell function declarations and
>>>function expressions apart more easily.
>>
>> And I recommend that you don't change, though a smaller indent
>> wouldn't hurt. Having the curly brackets lined up makes it easier to
>> see where the code body begins and ends, especially when the body
>> contains inner function definitions.
>
>However, as you have provably little if any theoretical knowledge of and
>practical experience with the programming languages in question, your
>recommendations should be weighed against that.
>
>There is an important difference between function declarations and function
>expressions that is _not_ syntactical, and it is wise to write source code
>so that such differences are easier to see.
>
>>>> return this.foo;
>>>> };
>>>>
>>>>
>>>> I mean if I add this piece of code into the class definition
>>>
>>>There are no classes in this case;
>>
>> Not true. See an earlier thread.
>
>In the earlier thread you already failed to provide convincing, let alone
>non-fallacious arguments that the terminology you prefer, in the way you
>prefer it, is useful in a technical discussion on the subjects that are on-
>topic here.

Hear ye, hear ye, all followers of comp.lang.javascript :-

It is *utterly forbidden* to disagree with Thomas Lahn.
There will be *no* discussion of other viewpoints whatsoever.


>>>prototype-based inheritance is used.
>>
>>>> I can call the getFoo in this way var poo = f.getFoo(); // "bar"
>>> ^ method
>>>
>>>> this.getFoo = function ()
>>>> {
>>>> return this.foo;
>>>> };
>>>>
>>>> So as a summary can somebody explain what is the advantage to use a
>>>> prototype in this exemple?
>>>
>>>Inherited methods can be reused. In this example, one advantage is that
>>>only one Function instance is created, which saves memory as compared to
>>>the instance method approach. It also saves runtime because the creation
>>>of that object only happens once. (There are also disadvantages.)
>>>
>>>> function P(foo) //Here is the class definition
>> <snip>
>>
>> No; here is the class's constructor function.
>
>Utter nonsense.

It *is* a constructor function.


>> The class's definition has to be deduced from the code,
>
>There is no class as there are no classes in the respective original
>language (what is created from that by compilers for virtual machines for
>optimization, indeed, is irrelevant in that regard).
>
>Therefore there is no ?class definition? to ?deduce? other than that which
>can only be ascribed to either too vivid imagination or utter wannabe-ness.
>The constructor function?s declaration, on the other hand, is plain to see,
>it can be looked up in authoritative reference material, and it is what this
>code is being parsed as by *all* relevant script engines.

From the code one can deduce what kind of objects are going to be
constructed. It would be advantageous if that were written down in the
program's documentation before coding, especially in a large-scale
project.

You could argue that the documentation should be called a kind
definition if you wish.


>> which is presumably why some people say ECMAScript, up to v5 at least, has
>> no classes.
>
>ECMAScript Editions 1 to 3, and 5.x, simply do not; neither have their
>implementations. (The abandoned Edition 4 working draft is irrelevant with
>regard to client-side code in Web browsers, but it must be excluded from
>that list.)
>
>However, ECMAScript Edition 6 and its implementations are going to, and they
>are going to have prototype-based inheritance as well.
<snip>

It is obvious that you, Thomas, are not familiar with C++ V-tables.
They do exactly the same job as an ECMAScript prototype chain but are
far more efficient and cannot be altered by the user code, or even
accessed.

V-tables are the obvious data structures to do the job but the
Standard allows compilers to use any means they wish. A compiler could
even use ECMAScript prototype chains, but wouldn't as it would be too
slow and unnecessarily complicated.


John

Christoph M. Becker

1/7/2015 3:42:00 PM

0

John Harris wrote:

> On Tue, 06 Jan 2015 19:03:33 +0100, "Christoph M. Becker"
> <cmbecker69@arcor.de> wrote:
>
>> John's argument might be regarded secondary (if not invalid) nowadays.
>
> It's still primary. Seeing where function definitions start and
> finish, quickly, is vital to maintenance programmers.

ACK. However, many contemporary editors are able to display vertical
lines joining the starting and ending line of a "block" ("indentation
guides"), what is IMO even clearer than aligned braces. Furthermore
there is code folding and "jump to matching brace" available even in non
graphical editors (e.g. vim).

--
Christoph M. Becker