Live data from Hacker News

Simple JavaScript Inheritance with Backbone

blog.usefunnel.com

1–10 of 19 posts

Re: Simple JavaScript Inheritance with Backbone

#3

You can also use CoffeeScript to get more "traditional" OOP from JavaScript.

CoffeeScript does have really nice syntax for OOP, but I think JavaScript deserves some lovin' too. Well-written JavaScript (such as Backbone) looks pretty beautiful. Another plus is that new devs (if you have to worry about that sort of thing) don't have to learn a new language.

Re: Simple JavaScript Inheritance with Backbone

#4

You can also use CoffeeScript to get more "traditional" OOP from JavaScript.

In fact, they're both implemented in the same way, using the "standard" JavaScript pattern for setting up a prototype chain. (With a few minor extra features). Here's the annotated source code:

http://documentcloud.github.com/backbone/docs/backbone.html#...

Re: Simple JavaScript Inheritance with Backbone

#5
When I first started with Javascript I used to think it was an awful excuse for a programming language, with its weird prototypal inheritance and functions pretending to be classes. The more I get to know it though, the more impressed I am by how all these features can be used and built upon to create an easy and flexible language. It has been taken in so many directions and I think Backbone is a prime example of how it can be built upon well. I love coding using the Backbone framework but I like that it's not the only option for doing things in an object oriented way.

Re: Simple JavaScript Inheritance with Backbone

#6

You can also use CoffeeScript to get more "traditional" OOP from JavaScript.

In fact, they're both implemented in the same way, using the "standard" JavaScript pattern for setting up a prototype chain. (With a few minor extra features). Here's the annotated source code: http://documentcloud.github.com/backbone/docs/backbone.html#...

That's really what I liked the most about the Backbone implementation. It stays very close to the standard JS pattern--essentially mirroring what someone would do if they wanted to setup the prototype chain and attach methods manually.

It asks you to name the constructor method 'constructor' (when it could've easily been called something short like 'init' instead, for brevity). This corresponds well with the standard 'constructor' property available on each object, which points back to the constructor function that created the object.

The '__super__' property is also a nice convenience, without doing something "fancy" like wrapping each method with logic that temporarily injects a 'super' property into the current instance for the duration of a method call. Although that's definitely clever and convenient, I prefer the simplicity of using '__super__'.

Re: Simple JavaScript Inheritance with Backbone

#7
post #6

Earlier quoted context omitted.

In fact, they're both implemented in the same way, using the "standard" JavaScript pattern for setting up a prototype chain. (With a few minor extra features). Here's the annotated source code: http://documentcloud.github.com/backbone/docs/backbone.html#...

That's really what I liked the most about the Backbone implementation. It stays very close to the standard JS pattern--essentially mirroring what someone would do if they wanted to setup the prototype chain and attach methods manually. It asks you to name the constructor method 'constructor' (when it could've easily been called something short like 'init' instead, for brevity). This corresponds well with the standard…

Interestingly enough, the fancy approach for calling "super()" is actually prohibitively expensive ... making any method call in a subclass far slower than it otherwise could be. I don't think it's a viable option for a baseline library.

http://www.broofa.com/2009/02/javascript-inheritance-perform...

Re: Simple JavaScript Inheritance with Backbone

#9
post #8

Slick but there has to be a way to do this without another utility. Backbone objects already have an extend method.

Yup, I agree. I recently submitted a pull request for changes I made to expose a primitive Base class in Backbone: https://github.com/documentcloud/backbone/pull/276

Re: Simple JavaScript Inheritance with Backbone

#10
I've just started down my path of really learning JavaScript. My first stop was "JavaScript: The Good Parts", and the author there really advocates using closures to define objects and enable encapsulation. It seems in practice this approach isn't used often (such as Backbone here), why is that?
Post reply on HN