Hi,

> Thanks a lot for those :). Are there any other concepts you would suggest
> for a guy with mostly a Ruby and C background? Things i might find differnet
> from the kind of coding im used to.

No worries.  Just very quickly off-the-cuff (and you'll find some
posts about this on niftysnippets), in addition to the things we've
already pointed out:

1. Tobie already pointed out the horror of implicit globals.

2. Binding and the "this" keyword.

3. Closures.

Some further items for the C programmer (I don't know anything about
Ruby):

4. Understand the scope chain.  (Closely related to closures, but good
to know separately as well.)

5. {} blocks _don't_ introduce new scope; only functions do that.

6. Regardless of where you put a var statement, all vars exist
function-wide.  For this reason, I strongly recommend putting all vars
at the beginning of the function (like you used to have to with C,
back in the day), to avoid misunderstandings.

7. Most of the places you're used to using NULL, use undefined.  You
can use null, but it's more common to use undefined.  When testing,
use "if (thingy)" where you're used to writing "if (thingy != NULL)"
in C, and similarly "if (!thingy)" for "if (thingy == NULL)".

8. The OR (||) operator doesn't return true or false; it returns the
left-hand value (unchanged) if that value can be coerced "true" (e.g.,
it's "truthy"), or the right-hand value if the left-hand value can't
be coerced "true" (it's "falsey").  This gets a huge amount of use in
JavaScript code, usually for supplying default values for something
that hasn't been defined.  Take this sort() function:

    function sort(comparator) {
        comparator = comparator || this.defaultComparator;
        // ...
    }

In C, that first line wouldn't make any sense because the result would
be -1 (TRUE) or 0 (FALSE); in JavaScript, the result is 'comparator'
if 'comparator' is truthy, or 'this.defaultComparator' if 'comparator'
is falsey.

Be careful about going too far with that, though.  For instance, if
you have an optional numeric argument and zero is a valid value for
the caller to pass in and 42 is the default, you can't use

   arg = arg || 42;

...to default it, because you'll use 42 instead of 0 if the caller
passes in 0 (zero is falsey).  Similarly, an empty string is falsey.

9. Properties are really cool, and you can get at them in many ways.
The following all retrieve the value of the 'bar' property of the
'foo' object and store it in 'x':

A) x = foo.bar;
B) x = foo['bar'];
C) s = 'bar'; x = foo[s];

That last one is convenient for when you don't know the name of the
property in advance.

10. There are no methods.  There are functions, and you can store
references to functions as properties of objects, and there is a
convenient syntax for calling those functions with "this" set as a
reference to the object during the course of the call, but there are
no methods.  "this" is effectively a special function argument,
nothing more.

11. "for..in" does not loop through the index values of an array, so
don't use it for that.  It loops through the names of the properties
on an object (which is very handy), including the names of properties
that refer to functions.

12. The ECMA spec allows for properties that can't be enumerated
(e.g., don't show up in "for..in"), but you can't mark your own
properties that way.  For instance, "for..in" on an array won't show
you the "length" property even though arrays have a length property.
(It also won't show you the "pop" property, even though arrays have a
"pop" property -- which references a function.  But it will show you
Prototype's additions to arrays, because we can't mark them not to be
included.  This is why for..in on arrays *seems* to loop through the
array's contents -- until you start extending arrays the way Prototype
does, and then suddenly you're getting function names.)

12. There is a difference between not having a property and having a
property whose value is undefined.  Referencing a property that
doesn't exist gives you undefined (not an error), though, so the
difference can be subtle.  The "in" keyword tests whether an object
has a property.  "delete" deletes a property entirely.

foo = {};            // Create an empty object
foo.bar = 42;        // Give it a 'bar' property
alert(foo.bar);      // Alerts "42"
alert('bar' in foo); // Alerts "true"
foo.bar = undefined; // Set 'bar' to undefined
alert(foo.bar);      // Alerts "undefined"
alert('bar' in foo); // Alerts "true"
delete foo.bar;      // Completely remove 'bar' property
alert(foo.bar);      // Alerts "undefined"
alert('bar' in foo); // Alerts "false"

Note that if we created foo in a way that it had a prototype with a
'bar' property, the second-to-last alert would be controlled by the
prototype's value for 'bar' and the last alert would be
"true" (because "in" looks in prototypes, too).  foo.hasOwnProperty
('bar') checks to see if foo has its *own* 'bar' property (as opposed
to one inherited from its prototype).

13. Prototypical inheritance is incredibly cool and powerful, but
don't think about it too much until you have time to think about it a
lot.

14. Prototype's extensions to Function are really, really cool and
well worth reading up on.

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On May 17, 10:42 am, Vinay Seshadri <yourstruly.vi...@gmail.com>
wrote:
> Hi Crowder,
>
> I actually did spend a couple of hours on the API a few months back when I
> had to write this JS code im talking about. I must admit though, that I
> conveniently concentrated only on those methods that I felt I required to
> accomplish what I had to. What I really need is to understand the basic
> rules and concepts used by and in JS.
>
> >Binding and the "this" keyword [2], and closures[3]
>
> Thanks a lot for those :). Are there any other concepts you would suggest
> for a guy with mostly a Ruby and C background? Things i might find differnet
> from the kind of coding im used to.
>
> On Sun, May 17, 2009 at 2:59 PM, T.J. Crowder <t...@crowdersoftware.com>wrote:
>
>
>
>
>
> > Hi,
>
> > > Thanks a lot Tobie! :) Ill check those out. Learning JS in detail is
> > > definitely a must do for me, AFTER this project :D.
>
> > FWIW, I suspect you'll save time even on this project if you take an
> > hour to read through the Prototype API[1] from beginning to end,
> > especially now that you've got your feet wet.  You may not have time
> > right now to read through Flanagan's or Crockford's books, but an hour
> > spent with the API will almost certainly pay for itself right away.
>
> > [1]http://prototypejs.org/api
>
> > Separately, can I also suggest you read up on two fundamental concepts
> > that may well trip you up otherwise:  Binding and the "this" keyword
> > [2], and closures[3].  I've linked some resources about that
> > (disclaimer: niftysnippets is my blog, not that I post to it much).
>
> > [2]
> >http://proto-scripty.wikidot.com/prototype:tip-using-an-instance-meth...
> > (also the articles linked at the bottom of [2])
> > [3]
> >http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html
>
> > HTH,
> > --
> > T.J. Crowder
> > tj / crowder software / com
> > Independent Software Engineer, consulting services available
>
> > On May 17, 10:04 am, Vinay Seshadri <yourstruly.vi...@gmail.com>
> > wrote:
> > > Thanks a lot Tobie! :) Ill check those out. Learning JS in detail is
> > > definitely a must do for me, AFTER this project :D.
>
> > > On Sun, May 17, 2009 at 12:02 PM, Tobie Langel <tobie.lan...@gmail.com
> > >wrote:
>
> > > > JavaScript has got this weird flaw, that if you forget to declare a
> > > > variable using the "var" keyword, the variable is declared in the
> > > > global scope instead of the current scope.
>
> > > > For example:
>
> > > > function foo() {
> > > >  bar = 1;
> > > >  var baz = 2;
> > > > }
>
> > > > foo();
> > > > console.log(bar);
> > > > // -> 1
> > > > console.log(baz);
> > > > // -> ReferenceError: baz is not defined
>
> > > > As a rule of thumb: Never declare a variable WITHOUT using the "var"
> > > > keyword. If you want to declare a global variable from inside a
> > > > function (which should be a sign you're doing something wrong), set it
> > > > as a property of the window object like so:
>
> > > > function foo() {
> > > >  window.bar = 1;
> > > > }
>
> > > > foo();
> > > > console.log(bar);
> > > > // -> 1
>
> > > > You should really get yourself a couple of books on JavaScript. I'd
> > > > strongly suggest getting:
>
> > > > - JavaScript: The Definitive Guide, Fifth Edition by David Flanagan,
> > > > and
> > > > - JavaScript: The Good Parts by Douglas Crockford
>
> > > > There's also a number of good books on Prototype out there:
>
> > > > -
> >http://prototypejs.org/2008/8/11/practical-prototype-and-scriptaculous,
> > > > and
> > > > -
> > > >http://prototypejs.org/2007/5/7/prototype-and-script-aculo-us-the-bun.
> > ..
>
> > > > Have fun!
>
> > > > Tobie
>
> > > --
> > > In Sport We Trust !!!
>
> --
> In Sport We Trust !!!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to