This thread is really neat - I am kind of late to the party though.
Tom, if you are sometime planning to give a talk on JavaScript I would
be interested in being your audience.
Regards,
Shantanu
> Classes are alright, actually, especially if by *class* you are not
> referring to a very rigid Java- or C++-like class. It's a matter of
> terminology. (See the next paragraph.) Say you want to create a certain type
> of widget. Dojo will let you instantiate or subclass a widget class. Dojo
> (which the author of all that awesome roundup contributes to) is a very deep
> collection of meta-programmed utilities. It makes up for JavaScript's lack
> of complex syntax (here I'm comparing it to Ruby) by using JavaScript's very
> flexible nature to do with function (or method) calls what other languages
> do with syntax.
>
> Although there is nothing called a class in JavaScript, you can think of
> JavaScript's constructor as the class of the objects it makes, because the
> constructor's prototype member sets up methods and elements that are
> inherited by one object from another. But of course there is no syntactic
> sugar like 'extend' or 'super'. As Crockford points out in Section 5.1 of
> his little book <
http://amzn.to/bWbofP>, the constructor is a pointless
> indirection. The good part is the prototype. Here's Crockford's way of
> creating an object and setting its prototype from an object passed to his
> singleton method Object.create:
>
> *Object.create = function(o) { // Earlier D.C. called it beget*
> * **// This create function should not be used to*
> * **// create arrays, because such created arrays*
> * **// would not have the special 'length' property.*
> * **function F() {}*
> * **F.prototype = o;*
> * **return new F();*
> *};*
>
> Eric on StackOverflow explained something <
http://bit.ly/9PiU5W> about
> an object
> creator <
http://javascript.crockford.com/prototypal.html> described in
> Crockford's *JavaScript: The Good Parts <
http://amzn.to/bWbofP>*. Note that
> in the book Crockford does not clearly describe the object creator all in
> one place in the book. Eric helped me understand some things that, imho,
> Crockford did not make clear. But still, I found what I thought were errors
> in Eric's code. I believe I have corrected those errors and extended the
> utility of Crockford's code with the following function, which probably
> should be a singleton method of Object:
>
> * function createNew() {*
> * **// A function to explain the new operator.*
> * **// var object = createNew(...);*
> * **// is equivalent to*
> * **// var object = new constructor(...);*
> * **//*
> * **// arguments: constructor function, followed by its*
> * // arguments*
> *** // return: a new instance of the*
> * // "constructor" kind of objects*
> *
> *
> * **// Preliminaries: convert arguments to a real array,*
> * **// get the constructor, and get the*
> * // arguments to the constructor.*
> * // Note: cheat by using 'new' to implement 'new'.*
> * **var args = Array.prototype.slice.call(arguments);*
> * **var constructor = args[0];*
> * **var constructorArgs = args.slice(1);*
> * *
> * **// Step 1: Create a new empty object instance linked to*
> * **// the prototype of provided constructor.*
> * **var hiddenLink = function(){};*
> * **hiddenLink.prototype = constructor.prototype;*
> * **// CORRECTED: 'object' was 'instance'.*
> * **var object = new hiddenLink();*
> * *
> * **// Step 2: Apply the constructor to the new instance*
> * **// and get the result.*
> * **// CORRECTED: 'instance' was 'result'.*
> * **// ADDED: arguments.slice(1))*
> * **//var instance = constructor.apply(object, args);*
> * **var instance = constructor.apply(object,*
> * constructorArgs);*
> * *
> * **// Step 3: Check the result, and choose whether to*
> * **// return it or the created instance.*
> * **if (typeof instance === 'object') {*
> * ** // CORRECTED: 'instance' was 'object'.*
> * ** return instance;*
> * **} else {*
> * ** // CORRECTED: 'object' was 'instance'.*
> * ** return object;*
> * **};*
> * };*
>
> One point of creating a generic object creator function or method is to make
> object creation more functional by obviating the use of the 'new' operator.
>
> Regards,
> Tom
>
tomelam.blogspot.comwww.tomelam.com
> Mobile:
9845222813
>
> I must create a system, or be enslaved by another man's.
> -William Blake
>
> On Fri, Nov 12, 2010 at 6:31 PM, Habibullah Pagarkar <
>
>
>
>
>
>
>
>
habibpagar...@gmail.com> wrote:
> > This is a very good set of resources. Thanks Tom! People fall easily into
> > the trap trying to implement classes in this fluid language instead of
> > thinking in terms of objects and prototypes.
>
> > On Fri, Nov 12, 2010 at 3:50 PM, Thomas Elam <
tome...@gmail.com> wrote:
>
> >> JavaScript explained<
http://lazutkin.com/blog/2009/mar/1/javascript-explained/>
>
> >> Regards,
> >> Tom
> >>
tomelam.blogspot.com
> >>
www.tomelam.com
> >> Mobile:
9845222813
>
> >> I must create a system, or be enslaved by another man's.
> >> -William Blake
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "bangalore-fp" group.
> >> To post to this group, send email to
bangal...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >>
bangalore-fp...@googlegroups.com<bangalore-fp%2Bunsubscribe@google
groups.com>
> >> .
> >
habibpagar...@gmail.com
> >
bangalore-fp...@googlegroups.com<bangalore-fp%2Bunsubscribe@google
groups.com>
> > .