Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Backbone "extend" function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Konstantin Kudryavtsev  
View profile  
 More options Oct 16 2012, 6:31 am
From: Konstantin Kudryavtsev <konstantin.k...@gmail.com>
Date: Tue, 16 Oct 2012 11:31:27 +0100
Local: Tues, Oct 16 2012 6:31 am
Subject: [JSMentors] Backbone "extend" function

Dear JS Mentors,

Could you please help me understand the use of Surrogate class in
Backbone's extend function
https://github.com/documentcloud/backbone/blob/master/backbone.js#L1436 .
In particular, why do this: http://jsfiddle.net/z4jMc/ if you can do that:
http://jsfiddle.net/8J3SZ/2/ (less code in the second case).

Really appreciate your help!

Cheers,
Konstantin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Buchanan  
View profile  
 More options Oct 16 2012, 9:52 am
From: Jonathan Buchanan <jonathan.bucha...@gmail.com>
Date: Tue, 16 Oct 2012 14:51:48 +0100
Local: Tues, Oct 16 2012 9:51 am
Subject: Re: [JSMentors] Backbone "extend" function

On 16 October 2012 11:31, Konstantin Kudryavtsev
<konstantin.k...@gmail.com>wrote:

> Dear JS Mentors,

> Could you please help me understand the use of Surrogate class in
> Backbone's extend function
> https://github.com/documentcloud/backbone/blob/master/backbone.js#L1436 .
> In particular, why do this: http://jsfiddle.net/z4jMc/ if you can do
> that: http://jsfiddle.net/8J3SZ/2/ (less code in the second case).

> Really appreciate your help!

> Cheers,
> Konstantin

The "surrogate" form puts the parent constructor's prototype in the child
constructor's prototype chain - this is the standard workaround for using
the behaviour of the "new" keyword to put a particular object in a
prototype chain to simulate inheritance, for compatibility where other
means of manipulating prototype chains (Object.create and __proto__) may
not be available.

Given this setup::

   var Parent = extend.call(Object, {})
   var Child = extend.call(Parent, {})
   var c = new Child()

...the following is true for the surrogate version, but not for the
_.extend() version::

   // Parent.prototype is in Child.prototype's prototype chain
   // Parent properties will be found by walking the prototype chain at
lookup time
   Child.prototype.__proto__ === Parent.prototype

Resulting differences for the surrogate version:

* objects created with a "child" constructor will also be instanceof their
"parent" constructor (and any other ancestor constructors)
* since parent prototype properties are resolved when looked up, any
changes made to the parent prototype will be accessible to child instances

Try this with both implementations::

   var Parent = extend.call(Object, {})
   var Child = extend.call(Parent, {})
   var c = new Child()
   Parent.prototype.newThing = true

   console.log('c is ' +
               (c instanceof Parent ? '' : 'not ') +
               'a Parent')
   console.log('Parent.prototype is ' +
               (Child.prototype.__proto__ === Parent.prototype ? '' : 'not
') +
               "in Child's prototype chain")
   console.log('Parent.prototype is ' +
               (c.__proto__.__proto__ === Parent.prototype ? '' : 'not ') +
               "in c's prototype chain")
   console.log('c ' +
               (typeof c.newThing == 'undefined' ? 'does not have' : 'has')
+
               ' access to newThing')

I also have an example of going from vanilla JS inheritance to this style
of inheritance sugar here [1]

Jonny.

[1] https://github.com/insin/concur/blob/master/README.rst


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Konstantin Kudryavtsev  
View profile  
 More options Oct 16 2012, 10:56 am
From: Konstantin Kudryavtsev <konstantin.k...@gmail.com>
Date: Tue, 16 Oct 2012 15:56:04 +0100
Local: Tues, Oct 16 2012 10:56 am
Subject: Re: [JSMentors] Backbone "extend" function

That helps. Thanks a lot!

KK

On Tue, Oct 16, 2012 at 2:51 PM, Jonathan Buchanan <


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Konstantin Kudryavtsev  
View profile  
 More options Nov 9 2012, 7:11 am
From: Konstantin Kudryavtsev <konstantin.k...@gmail.com>
Date: Fri, 9 Nov 2012 12:11:34 +0000
Local: Fri, Nov 9 2012 7:11 am
Subject: Re: [JSMentors] Backbone "extend" function

Hi Jonathan,

I found in the documentation for concur that you are referring to
Backbone-style inheritance as "infectious". I haven't come across this term
anywhere else, except for in medical context. But then, I haven't really
seen any other terms used to describe this fairly popular style of
inheritance in JS. Just wondering, where does it originate from?

Thanks,
Konstantin

On Tue, Oct 16, 2012 at 3:56 PM, Konstantin Kudryavtsev <


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Buchanan  
View profile  
 More options Nov 21 2012, 9:59 am
From: Jonathan Buchanan <jonathan.bucha...@gmail.com>
Date: Wed, 21 Nov 2012 14:59:09 +0000
Local: Wed, Nov 21 2012 9:59 am
Subject: Re: [JSMentors] Backbone "extend" function

Hi Konstantin,

I described it as "infectious" for cuteness more than anything else, as you
use <constructor>.extend() to do all the legwork in terms of setting up the
prototype object and prototype chain for your new constructor and the final
thing extend() does is to add itself to the new child constructor as an
'extend' property, thus "infecting" it for further extension :)

    childConstructor.extend = this.extend

Jonny.

On 9 November 2012 12:11, Konstantin Kudryavtsev
<konstantin.k...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »