12/07/2012

Notes on JavaScript–part 1 Types

I’ve been reading through the JavaScript documentation on the Mozilla Dev site (http://developer.mozilla.org), there is a great article on A re-introduction to JavaScript which is a great article.

For my own reference I’ve made some notes on some of the things mentioned in the article, if I am wrong or have miss understood something, hopefully some one will let me know. Ok first off Types.

There are five types in JavaScript, six if you include the Error type, they are:

  • Number
  • String
  • Boolean
  • Object
  • Null
  • Undefined

Functions, Arrays and Dates are also objects, so the structure of the types in JavaScript should be:

  • Number
  • String
  • Boolean
  • Object
    • Function
    • Array
    • Date
    • RegEx
  • Null
  • Undefined

JavaScript treats Functions as object, which coming from a ActionScript 3 background seems at first a bit odd, but thinking about it this gives a lot of power to Functions.

One of the biggest shifts in how to approach JavaScript from an ActionScript 3 background, is getting use to the idea that there is no Class within JavaScript. You can create a ‘class’ using a function, e.g.

   1:  function Car() {
   2:   
   3:  }
   4:  var myCar = new Car();



You can use the Prototype object replicate the functionality of classes in JavaScript, but more on that later.

No comments: