Most awesomest roundup of JavaScript ninjaism

12 views
Skip to first unread message

Thomas Elam

unread,
Nov 12, 2010, 5:20:24 AM11/12/10
to bangal...@googlegroups.com
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

Rishav Rastogi

unread,
Nov 12, 2010, 6:38:02 AM11/12/10
to bangal...@googlegroups.com
Awesome!! Thanks

Rishav 
--
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.
For more options, visit this group at http://groups.google.com/group/bangalore-fp?hl=en.



--
Rishav Rastogi
http://twitter.com/rishavrastogi
Cell : +91 9980616888
Gtalk : rishav.rastogi
Skype : rishavrastogi

Habibullah Pagarkar

unread,
Nov 12, 2010, 8:01:58 AM11/12/10
to bangal...@googlegroups.com
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 <tom...@gmail.com> wrote:
--
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.
For more options, visit this group at http://groups.google.com/group/bangalore-fp?hl=en.



--
Habibullah Pagarkar
habibp...@gmail.com

http://www.cs.jhu.edu/~habib
http://habib.posterous.com/
http://twitter.com/mhabibp

Thomas Elam

unread,
Nov 12, 2010, 12:04:13 PM11/12/10
to bangal...@googlegroups.com
Habib,

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, 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 about an object creator described in Crockford's JavaScript: The Good Parts. 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.com
www.tomelam.com
Mobile: 9845222813

I must create a system, or be enslaved by another man's.
-William Blake



Shantanu Kumar

unread,
Dec 13, 2010, 12:59:42 PM12/13/10
to bangalore-fp
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

On Nov 12, 10:04 pm, Thomas Elam <tome...@gmail.com> wrote:
> Habib,
>
> 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>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/bangalore-fp?hl=en.
>
> > --
> > Habibullah Pagarkar
> > habibpagar...@gmail.com
>
> >http://www.cs.jhu.edu/~habib
> >http://habib.posterous.com/
> >http://twitter.com/mhabibp
>
> >  --
> > 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>
> > .

Thomas Elam

unread,
Dec 14, 2010, 11:31:41 AM12/14/10
to bangal...@googlegroups.com
Shantanu,

I might give a talk, especially if there is enough interest.  But in the next 2 - 3 months it probably won't be possible for me.

In the meantime you can check out my object-oriented JavaScript examples on github.  Let me know if you find any mistakes!
Reply all
Reply to author
Forward
0 new messages