Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26472

Re: Why use prototype in this example

From "Christoph M. Becker" <cmbecker69@arcor.de>
Newsgroups comp.lang.javascript
Subject Re: Why use prototype in this example
Date 2015-01-06 23:53 +0100
Organization solani.org
Message-ID <m8hp1m$oqg$1@solani.org> (permalink)
References <m8hnjv$d29$1@dont-email.me>

Show all headers | View raw


Tony Johansson wrote:

> Here I have two examples. The difference between these is that in
> example 1 I create sayHello as a prototype while in
> exemple 2 I add sayHello as a function in the class Person
> 
> I mean in both example 1 and 2 I create an instance of Person.
> I can't see any point to use a prototype compared to add methods to the
> class?

Thomas 'Pointed Ears' Lahn already gave a good answer in
<news:1590597.KOVAYohEvS@PointedEars.de>:

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

-- 
Christoph M. Becker

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why use prototype in this example "Tony Johansson" <johansson.andersson@telia.com> - 2015-01-06 23:29 +0100
  Re: Why use prototype in this example "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-06 23:53 +0100
  Re: Why use prototype in this example Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-07 04:25 +0100

csiph-web