Javascript

For discussions about game development that does not fit in any of the other topics.
Post Reply
Cayle
Posts: 272
Joined: Fri Jul 03, 2009 4:45 am

Javascript

Post by Cayle »

I have to learn javascript for work. I don’t have to do any professional level programming in js, but I need to understand the language enough to speak with javascript programmers in their native tongue. So what’s it like? I've heard that it is weird for people coming from java and c#, but Python also has some of the supposedly weird concepts like dynamic and duck typing, etc. and I'm a Python voodoo virtuoso (just look at Angela's code sometime. It fully exploits multiple inheritance, dynamic programming and map-reduce). The js code that I've seen thus far looks nasty, simply nasty; and disorganized. It reminds me of Torque-script; and not in a good way. But this could be an object oriented bias.

I also know that there are a LOT of people who like the language, so it must be good at what it does. What are the main ways that javascript differs and what are the things that I need to understand to grok the language?
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Javascript

Post by hallsofvallhalla »

Javascript is different but not a python different. To me if C# was Home base, something like PHP would be at first base, Javascript might be around 2nd or maybe left field, python would be in the stands, and Ruby would be at home watching the game :P

It is quite different when you get into the OOP side of it. The manipulation of the DOM is lovely but hard to get the head wrapped around in the beginning. It is very lose typed as well. Put a semi colon or don't. Cast and initialize the variable or don't.

My Nodejs tutorials(HallsNode) goes into some javascript that might help. I also am releasing the continuation of my browser tutorials that goes head first into javascript tonight.

To me it is not the Javascript syntax that can be a challenge to learn but the libraries and the DOM.
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Javascript

Post by Ark »

Everything is object oriented, you can bind a function to numbers for example:

Code: Select all

Number.prototype.multiplyByTen = function() {
  return this * 10;
}

(12).multiplyByTen() // 120
Javascript can be easy or very complex. A great thing though is that there are tons of frameworks and libraries for javascript.
Orgullo Catracho
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Javascript

Post by Jackolantern »

The 3 most important things about Javascript, at least in my opinion:

1. It is Prototype-based instead of traditional OOP: the typically considered "best" way of making an object is to just build it. Data goes directly into the object, and methods go onto the Prototype (like Ark showed). Methods can go directly into the object, but they will be repeated in each object, thus wasting memory.

2. Functions are first-class objects: Functions can be assigned to variables and passed around like data. This is used very often in JS, such as callback functions for asynchronous function calls.

3. Javascript objects are the best way to represent data in JS: You can create ad-hoc data structures with complete freedom. You can have objects inside of objects inside of arrays inside of objects. JS gives you plenty of tools to deal with it.
The indelible lord of tl;dr
Cayle
Posts: 272
Joined: Fri Jul 03, 2009 4:45 am

Re: Javascript

Post by Cayle »

OK, I ordered a book on the subject (on my Employer's dime). It sounds like a language that I can have fun with, once I wrap my head around the use of the $ and the fact that I've seen classes where the this references in the methods was not the class (actually the instance), but rather the calling class. THAT is wonky, but I'm sure it can be abused in a fun way; much like thew way I abuse callable objects in Python.

It's for use with a very specific framework.

But can I import antigravity with it?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Javascript

Post by Jackolantern »

Ohhhh, Javascript is possibly one of the most fun languages you will ever use. Honestly, it has about 8 different full OOP implementations. I personally use the Module Pattern. But you can mix and match them in the craziest of ways. And believe it or not, there is still a frontier out there. You may very well find a usage of the language no one has ever found before, particularly when it comes to OOP. People are still finding new things every other month or so, typically by combining or nesting OOP strategies. Closures are very, very interesting and powerful, and basically that and functions as first-class objects are the foundation of JS's asynchronous nature.

I used to hate JS, but now I am quickly falling in love with it while working with node and jQuery together. That is one powerful combination!

Oh, and the '$' in Javascript is just a variable. Like a handful of languages, variable names can start with a dollar sign, and some libraries take advantage of that. In jQuery, the $ is just an alias to jQuery(), which is the main entrance to the API. There are options to not use this, though, if you are using a library which demands use of it.
The indelible lord of tl;dr
Post Reply

Return to “General Development”