Modern Javascript crash course

A very short crash course in Modern Javascript. Learn about functions, objects, how to create objects from functions, clojures and modules.

Variables

var a; //defines a variable a that has the value of undefined
       //which is a reserved word/value in js
var b = 1;
var c , d = 2, e, f = null; //multiple variables on one line
g = 10;  //variables initialized without var will be global
         //variables even if they are defined inside functions or
         //code blocks {}.
         //variables defined with var will have function scope: they
         //will be available only in the function in which they are
         //defined.
         //Variables defined with var but outside of any function
         //will be global variables.
         //There is no block {} scope. All variables defined in a
         //function will act as if they were defined at the top of
         //the function.
var h == 1;
if (h == 1) {
 var i = 2;
} else {
 var j = 3;  // no block {} scope. j is initialized before any code is
             //executed.
};
console.log(j); //prints 3. j exists and is initialized even if the
                //else branch is never executed.

Define an array

var fruits = [];  //empty array
var colors = ["blue", "red", "green"];
console.log(colors[1]); //prints red in console.

Define an object

var config = {};  //object without any properties.
var module = {"name" : "init",  //string property.
              "order": 1,       //number property.
              "children" : [],  //array property.
              activated  : false, //you can skip "" from properties
                                  //names if the name is not a
                                  //reserved word.
              "onStart" : function(){console.log("init started.")}
                          //function property a.k.a method.
             };
console.log(module.name);    //prints init.
console.log(module["name"]); //prints init. Objects can be accessed
                             //as if they were associative arrays.
var tmp = "name";
console.log(module[tmp]);    //prints init.

Define a function

function run() {
  console.log("clasic function definition");
};
run();

var run2 = function(){
  console.log("variable-like function definition");
};
run2();  //in js functions are values like any other value. Because
         //of this you can assign a function definition to a
         //variable. Then you can call that function by using the
         //variable's name.
         //Because function can be stored in variables you can pass
         //functions as arguments to another function or you can
         //store functions as an object property.
var obj = {}      //empty object
obj.doRun = run2; //define a method doRun and assign the
                  //function run2 to be used as the method.
obj.doRun();      //call the method/function run2. Notice the ().

Functions are objects

//although functions and objects are defined in different ways,
//internally js stores functions in objects. So when you do:
var sum = function(a, b) {return a + b};
//what actually happens is:
var sum = {prototype :   -some object-,
           code      : -code to be runned when sum() si called:
                        function(a, b) {return a + b}-,
           ...
          };
//So you see that js is building a special object that can store a
//function. Not sure about the actual name of the code property
//but it's a hidden property anyway (you can't access it from code).
//From the properties stored in this object, js knows if the object
//holds a function or if it's a regular object.

//Since functions are objects you can do with them everything that
//you can do with an object: assign it to a variable or pass it as an
//argument to another function.

-to be continued-

Lasă un răspuns

Adresa ta de email nu va fi publicată.