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
Reuse code, prototype inheritance or module pattern?
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
  17 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
 
Juan Felipe Alvarez Saldarriaga  
View profile  
 More options Mar 28 2012, 12:33 pm
From: Juan Felipe Alvarez Saldarriaga <nebi...@gmail.com>
Date: Wed, 28 Mar 2012 11:33:51 -0500
Local: Wed, Mar 28 2012 12:33 pm
Subject: Reuse code, prototype inheritance or module pattern?

Hi! :),

I'm trying to figure it out which one is the best way to reuse code, I'm reading about Module Pattern and Prototype Inheritance Pattern, seems like Modules are the hot stuff these days, so I made a little example about how to implement this thing, but seems like is a little bit complicated, at least isn't easy as Prototype Inheritance, what do you guys think about these?.

Here's my example: http://jsfiddle.net/JjmGW/3/

I read a lot of stuff, these are my picks about these:

* http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
* http://briancray.com/2012/02/23/javascript-module-pattern/
* http://snook.ca/archives/javascript/no-love-for-module-pattern
* https://github.com/shichuan/javascript-patterns/blob/master/object-cr...
* http://addyosmani.com/resources/essentialjsdesignpatterns/book/#detai...

I'm still thinking about Prototype Inheritance Pattern, but with some syntax sugar like Crockford's: http://www.crockford.com/javascript/inheritance.html

Thanks for any help.

--
Juan Felipe Alvarez Saldarriaga
http://juan.im
Twitter: @nebiros
GTalk: nebi...@gmail.com
Skype: jfasaldarriaga


 
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.
Juan Felipe Alvarez Saldarriaga  
View profile  
 More options Mar 28 2012, 1:10 pm
From: Juan Felipe Alvarez Saldarriaga <nebi...@gmail.com>
Date: Wed, 28 Mar 2012 10:10:25 -0700 (PDT)
Local: Wed, Mar 28 2012 1:10 pm
Subject: Re: Reuse code, prototype inheritance or module pattern?

About Prototype Inheritance Pattern and reading Andrea Giammarchi's essay
(http://www.3site.eu/doc/), I came up with this:
http://jsfiddle.net/d3eqQ/, I don't know if is some performance/bad
practice issue when I add "extend" method to Function object.

On Wednesday, March 28, 2012 11:33:51 AM UTC-5, Juan Felipe Alvarez


 
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.
Samuel Richardson  
View profile  
 More options Mar 28 2012, 5:37 pm
From: Samuel Richardson <s...@richardson.co.nz>
Date: Thu, 29 Mar 2012 08:37:04 +1100
Local: Wed, Mar 28 2012 5:37 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

The way I've internalised this (and I'm not sure if it's correct, please
feel free to jump in)

* The module pattern creates multiple instances of functions instead of
using the prototype chain, thus consuming more memory when instances of it
are created using the "new" function.

* The module pattern is good for creating "singletons" though.

* The module pattern is good for namespacing code (i.e. use it to wrap your
"class" definition that gets instanced, this is somewhat similar to how I
use CommonJS modules)

Samuel Richardson
www.richardson.co.nz | 0405 472 748

On Thu, Mar 29, 2012 at 4:10 AM, Juan Felipe Alvarez Saldarriaga <


 
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.
Juan Felipe Alvarez Saldarriaga  
View profile  
 More options Mar 28 2012, 5:52 pm
From: Juan Felipe Alvarez Saldarriaga <nebi...@gmail.com>
Date: Wed, 28 Mar 2012 16:52:54 -0500
Local: Wed, Mar 28 2012 5:52 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

Hi!,

Well my idea is to choose the best option for reuse code, you can actually inherit from another module like this example (http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Dep...

var MODULE_TWO = (function (old) {
var my = {}, key;

for (key in old) {
if (old.hasOwnProperty(key)) {
my[key] = old[key];

}
}

var super_moduleMethod = old.moduleMethod;
my.moduleMethod = function () {
// override method on the clone, access to super through super_moduleMethod

};

return my;

}(MODULE));

About the memory use, yes, some of the articles below say that, but I think for the reuse code approach isn't matter, at least if you have a really large code base. Maybe the prototype chain approach it's better, but what about multiple inheritance?. I was looking how Google Closure (http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.s...) do this and I came up with this: http://jsfiddle.net/n98dq/

var App = App || {};

App.Util = (function ($) {
  var module = {};

  function inherits(child, parent) {
    function I(){};
    I.prototype = parent.prototype;
    child.prototype = new I;
    child.prototype.constructor = child;
  }

  module.inherits = inherits;

  return module;

}(window.jQuery));

function A() {}
A.prototype.bark = function () {
  console.log("A:bark");

};

function B() {}

App.Util.inherits(B, A);

var b = new B();
// inspect.
console.log(b);
// bark!.
b.bark();

--
Juan Felipe Alvarez Saldarriaga
http://juan.im
Twitter: @nebiros
GTalk: nebi...@gmail.com
Skype: jfasaldarriaga


 
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.
Johannes Nel  
View profile  
 More options Mar 28 2012, 5:57 pm
From: Johannes Nel <johannes....@gmail.com>
Date: Wed, 28 Mar 2012 22:57:04 +0100
Local: Wed, Mar 28 2012 5:57 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

why not just use the closure goog.inherits function then.
For memory to become an issue you don't need a large code base, you need
many instances or a low powered device.

On Wed, Mar 28, 2012 at 10:52 PM, Juan Felipe Alvarez Saldarriaga <

--
j:pn
\\no comment

 
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.
Juan Felipe Alvarez Saldarriaga  
View profile  
 More options Mar 28 2012, 6:19 pm
From: Juan Felipe Alvarez Saldarriaga <nebi...@gmail.com>
Date: Wed, 28 Mar 2012 17:19:23 -0500
Local: Wed, Mar 28 2012 6:19 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

Hey!,

Well, if all my code is based in classes, isn't the same thing? I mean, if I choose prototype inheritance using Google Closure, I will start creating instances everywhere, one of the things that I try to achieve is this:

* A class with some basic code, like an abstract class.
* This "abstract class" in the constructor will have an options object to change some things inside.
* Inherit from this "abstract class" to get all normal behavior.
* The child class can change the "abstract class" behavior calling the "abstract class" constructor with different options.
* The child class can override methods from the "abstract class".

Any thoughts?, maybe you guys can tell me if there's some library or script to achieve this.

Thanks.

--
Juan Felipe Alvarez Saldarriaga
http://juan.im
Twitter: @nebiros
GTalk: nebi...@gmail.com
Skype: jfasaldarriaga


 
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.
Johannes Nel  
View profile  
 More options Mar 28 2012, 6:31 pm
From: Johannes Nel <johannes....@gmail.com>
Date: Wed, 28 Mar 2012 23:31:28 +0100
Local: Wed, Mar 28 2012 6:31 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

I use closure in advanced mode so I am tied to a specific way of
constructing classes, but the benefits are enormous. The module pattern is
not efficient, this is known, modifying prototypes on the fly is also an
iffy practice IMO.
I think what you might be after is the mixin strategy. Remember that scope
in javascript is "violently" mutable,  and mixins trade of this
(in)glorious language feature.
http://en.wikipedia.org/wiki/Mixin

On Wed, Mar 28, 2012 at 11:19 PM, Juan Felipe Alvarez Saldarriaga <

--
j:pn
\\no comment

 
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.
Juan Felipe Alvarez Saldarriaga  
View profile  
 More options Mar 28 2012, 6:37 pm
From: Juan Felipe Alvarez Saldarriaga <nebi...@gmail.com>
Date: Wed, 28 Mar 2012 17:37:02 -0500
Local: Wed, Mar 28 2012 6:37 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

Nice!,

Yes, I think about mixins too, but don't know how to implement it, yet, about "closures in advanced mode", do you have some example about this approach?.

Thank you.

--
Juan Felipe Alvarez Saldarriaga
http://juan.im
Twitter: @nebiros
GTalk: nebi...@gmail.com
Skype: jfasaldarriaga


 
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.
Jay Young  
View profile  
 More options Mar 28 2012, 6:38 pm
From: Jay Young <jayyoung9...@gmail.com>
Date: Wed, 28 Mar 2012 15:38:47 -0700 (PDT)
Local: Wed, Mar 28 2012 6:38 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

On Wednesday, March 28, 2012 6:31:28 PM UTC-4, Johannes Nel wrote:

> scope in javascript is "violently" mutable

Ahahahaha.  I love it.  I've never heard it described that way.

 
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.
Johannes Nel  
View profile  
 More options Mar 28 2012, 6:43 pm
From: Johannes Nel <johannes....@gmail.com>
Date: Wed, 28 Mar 2012 23:43:08 +0100
Local: Wed, Mar 28 2012 6:43 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

not "closures in advanced mode"

I use google closure with advanced mode compilation :)

On Wed, Mar 28, 2012 at 11:37 PM, Juan Felipe Alvarez Saldarriaga <

--
j:pn
\\no comment

 
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.
Stefan Weiss  
View profile  
 More options Mar 28 2012, 7:12 pm
From: Stefan Weiss <we...@foo.at>
Date: Thu, 29 Mar 2012 01:12:05 +0200
Local: Wed, Mar 28 2012 7:12 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?
On 2012-03-29 00:38, Jay Young wrote:

> On Wednesday, March 28, 2012 6:31:28 PM UTC-4, Johannes Nel wrote:

>     scope in javascript is "violently" mutable

> Ahahahaha.  I love it.  I've never heard it described that way.

That's probably a good thing, because as far as I know scope in JS is
completely immutable (barring eval acrobatics). "Scope" refers to the
lexical environment in which variables and functions are resolved; it is
directly related to the program structure and exists independently of
the conditions at runtime. What Johannes probably meant was the "this"
value, which is the exact opposite: it cannot always be inferred from
the program structure and depends instead on how a function is called at
runtime. This is also sometimes referred to as "context", but never as
"scope". Or at least, it shouldn't be, if you want to keep your
terminology straight.

- stefan

--
LOAD"Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn!",8,1
RUN!


 
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.
Johannes Nel  
View profile  
 More options Mar 28 2012, 7:27 pm
From: Johannes Nel <johannes....@gmail.com>
Date: Thu, 29 Mar 2012 00:27:27 +0100
Local: Wed, Mar 28 2012 7:27 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

what you say this is true in cs terms and yes, i was referring to the
resolution of the this. Although (and by no means would I recommend doing
this) __proto__ was another way to cheat the system.

--
j:pn
\\no comment

 
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.
Claus Reinke  
View profile  
 More options Mar 29 2012, 4:08 am
From: "Claus Reinke" <claus.rei...@talk21.com>
Date: Thu, 29 Mar 2012 10:08:59 +0200
Local: Thurs, Mar 29 2012 4:08 am
Subject: Re: [JSMentors] Reuse code, prototype inheritance or module pattern?

> I'm trying to figure it out which one is the best way to reuse code,
> I'm reading about Module Pattern and Prototype Inheritance Pattern,
> seems like Modules are the hot stuff these days,

Both? All three? Depends on the problem?-)

module pattern:
    local variable scope
modules:
    namespaces with import/export/separate files
prototypes:
    inheritance by delegation/sharing

So, your inheritance chains go into modules (possibly more than
one module per chain), which probably use the module pattern
and some module loader (unless there is a predefined module
system, as in nodejs). A simplified form of the module pattern
might be used to provide local scope for defining prototype
chains in (eg, for private variables).

Module pattern might be used less when block-scoping arrives,
modules and loaders will then be built in, prototype chains may
have class sugar:

    http://wiki.ecmascript.org/doku.php?id=harmony:proposals

Claus

PS: old post of mine on module pattern and (in-browser) modules
http://libraryinstitute.wordpress.com/2010/12/01/loading-javascript-m...


 
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.
Tony Brown  
View profile  
 More options Mar 28 2012, 7:08 pm
From: Tony Brown <tony.brown....@gmail.com>
Date: Wed, 28 Mar 2012 19:08:40 -0400
Local: Wed, Mar 28 2012 7:08 pm
Subject: Re: [JSMentors] Re: Reuse code, prototype inheritance or module pattern?

I was reading your blog this morning, good stuff

On Wed, Mar 28, 2012 at 6:43 PM, Johannes Nel <johannes....@gmail.com>wrote:

...

read more »


 
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.
Juan Felipe Alvarez Saldarriaga  
View profile  
 More options Mar 29 2012, 8:43 am
From: Juan Felipe Alvarez Saldarriaga <nebi...@gmail.com>
Date: Thu, 29 Mar 2012 07:43:05 -0500
Local: Thurs, Mar 29 2012 8:43 am
Subject: Re: [JSMentors] Reuse code, prototype inheritance or module pattern?

Thank you Claus!, good thoughts :).

Quick question: There's some advantage between return an object or return a "class" http://jsfiddle.net/JjmGW/3/ from a module? performance issues?.

Thanks.

--
Juan Felipe Alvarez Saldarriaga
http://juan.im
Twitter: @nebiros
GTalk: nebi...@gmail.com
Skype: jfasaldarriaga


 
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.
Miller Medeiros  
View profile  
 More options Mar 29 2012, 10:56 am
From: Miller Medeiros <lis...@millermedeiros.com>
Date: Thu, 29 Mar 2012 10:56:20 -0400
Local: Thurs, Mar 29 2012 10:56 am
Subject: Re: [JSMentors] Reuse code, prototype inheritance or module pattern?
My advice is:

 - Split things into smaller functions/methods/objects/modules, making it
easier to overwrite and reuse.
 - Avoid inheritance as much as possible, not because of performance or
anything similar, but because inheritance is overrated and can make things
more complex than needed.
 - Avoid private members as much as possible since it makes it
"impossible" to overwrite the behavior.
 - Avoid "classes" (constructor functions) as much as possible since in
many times what you really need is a function, not a class. Also sometimes
you are sure you will only need a single instance, some people might say
that singletons/static classes are an anti-pattern but it varies depending
on each case.
 - Avoid plugins, your code should be "modular", treat everything as an
individual piece (you should be able to reuse same code in a diff project
without any configuration), don't augment objects you don't own.
 - (ab)use `Function.call` and `Function.apply`.

"Sometimes, the elegant implementation is just a function. Not a method.
Not a class. Not a framework. Just a function." - John Carmack

More details about the things I said above in case anyone is interested:

 - http://blog.millermedeiros.com/keep-your-modules-and-functions-small/
 -
http://blog.millermedeiros.com/a-case-against-private-variables-and-f...
ns-in-javascript/
 -
http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-java...
-mixins/
 - http://pyvideo.org/video/880/stop-writing-classes (very good
presentation)

Cheers.

On 3/29/12 4:08 AM, "Claus Reinke" <claus.rei...@talk21.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.
Scott Sauyet  
View profile  
 More options Mar 29 2012, 11:05 pm
From: Scott Sauyet <scott.sau...@gmail.com>
Date: Thu, 29 Mar 2012 20:05:43 -0700 (PDT)
Local: Thurs, Mar 29 2012 11:05 pm
Subject: Re: Reuse code, prototype inheritance or module pattern?

Miller Medeiros wrote:
>  - Split things into smaller functions/methods/objects/modules, making it
> easier to overwrite and reuse.
>  - Avoid inheritance as much as possible, not because of performance or
> anything similar, but because inheritance is overrated and can make things
> more complex than needed.

OK so far.

>  - Avoid private members as much as possible since it makes it
> "impossible" to overwrite the behavior.

But that's really the point of private methods.  If a function is
meant to be used as an implementation detail, it should be kept
private.  No one should ever be able to override anything but the
public behavior.  I did read your blog post on the subject, and I'm
not convinced.  My main problem is that you get stuck with the public
API you present.  The more of it you expose (even if you use leading
underscores as a warning) the less flexibility you have in changing
the implementation later.

>  [ ... ]

  -- Scott

 
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 »