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


Groups > comp.lang.javascript > #17965

Re: Does copying the prototype reference from another constructor function maintain the prototype chain?

Newsgroups comp.lang.javascript
Date 2013-01-05 13:23 -0800
References <kc8jjg$k07$1@speranza.aioe.org>
Message-ID <f5f9f301-ae73-4acb-9ba8-e4843129c4bd@googlegroups.com> (permalink)
Subject Re: Does copying the prototype reference from another constructor function maintain the prototype chain?
From Luc Yen <luc@goal.tw>

Show all headers | View raw


GGolf於 2013年1月5日星期六UTC+8下午3時09分34秒寫道:
> I've been a Javascript dabbler for a few years, and have just embarked 
[...]
> Ninja.prototype = new Person();
> However, I notice that even the following statement makes all the test 
> statements pass:
> Ninja.prototype = Person.prototype;
> Does it mean that using "Person.prototype" can actually be used instead 
> 
> of "new Person()"?

The later construct has a problem. If you later extend new method for your ninjas

    Ninja.prototype.newMethod = function() {...}

It actually defined on Person's prototype and affect all your people instances. 
And think that if we have a SuperNinja that extends Ninja, you'll end up writing

    SuperNinja.prototype = Ninja.prototype; // Chaos, and again...
    NinjaMaster.prototype = SuperNinja.prototype; // Chaos

Now they point to the same Person.prototype(which in turn is a simple instance of Object).

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


Thread

Does copying the prototype reference from another constructor function maintain the prototype chain? GGolf <invalid@invalid.com> - 2013-01-05 12:39 +0530
  Re: Does copying the prototype reference from another constructor function maintain the prototype chain? JJ <jaejunks@nah.meh> - 2013-01-05 10:57 +0000
    Re: Does copying the prototype reference from another constructor function maintain the prototype chain? David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 06:44 -0800
  Re: Does copying the prototype reference from another constructor function maintain the prototype chain? David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 06:43 -0800
  Re: Does copying the prototype reference from another constructor function maintain the prototype chain? John G Harris <john@nospam.demon.co.uk> - 2013-01-05 15:53 +0000
    Re: Does copying the prototype reference from another constructor function maintain the prototype chain? David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 08:51 -0800
  Re: Does copying the prototype reference from another constructor function maintain the prototype chain? Scott Sauyet <scott.sauyet@gmail.com> - 2013-01-05 12:25 -0800
  Re: Does copying the prototype reference from another constructor function maintain the prototype chain? Luc Yen <luc@goal.tw> - 2013-01-05 13:23 -0800

csiph-web