"fallenrogue" Under Leon's hat.

Wed Dec 16

JavaScript Part 1 - hello JavaScript!

JavaScript is an object oriented scripting language.

It is a dialect of the ECMAScript standard.

It is dynamic, loose typed and prototype-based.

It has first-class functions. That means a function is an object in JavaScript. You can treat it as such.

If you’re looking for some history of the language and an introduction of the “why” check out Mozilla’s A re-introduction to JavaScript And once you’ve ready that or if you’re skipping it please continue…

It’s time to say hello to one of my favorite languages again. This time from my perspective. If there’s something you love, I believe you have little reason not to share it with the world. Do I want everyone to start programming in JavaScript? Well, that’s a lofty goal and not really my point here. My point is more to share my passion and clear up a few misconceptions.

So, play along with me friends… first task is to create a way to play. There are many but I’m going for low barrier for entry. Open firefox and download (if you don’t have it) firebug.

Now that you’ve got a way to play with JavaScript and the DOM, let’s start building a better understanding the features of the language. Create a blank HTML page and open it in Firefox. This will be the blank canvas with which we experiment with the language in a REPL style system via Firefox and Firebug. Let’s play with the loose type system first. Try assigning a variable a numeric value and then assign the same variable a string.

See? Type is loose.

Wanna see why we functions are “first-class” or “objects”? Let’s create a function. The syntax for creating a function is…

function [name](params[,n]){
    //method body
}

Now, why is name optional? Because functions, even without a name, are still objects. A common practice is to keep the function anonymous but assign it to a variable for later use.

var sayHello = function(say){alert(say);}

If you type both types of function into firebug you’ll see…

That they can be called the same way and as objects can be passed around just as you would any other type of object.

Internet darling jQuery makes extensive use of this feature in its implementation. It typically calls this practice “accepting a callback” which is just another way of saying “give me a function to call when I’m done doing whatever I’m doing and I’ll call it. I will also pass that function data related to my initial task.”

Finally, what does it mean to be “prototype based”? Well, that’s where we’re going to start Part 3, so until next time write something fun using Firebug and JavaScript. Try using the DOM’s document object to write something to the page.

See, good times. gooooood times. :)

Comments (View)
blog comments powered by Disqus