js-intro



js-intro

0 0


js-intro

Intro into JS

On Github greenify / js-intro

Intro to JavaScript

(ECMaScript V)

seb@wilzba.ch

Writing JS code

JS Bin

Simple gotchas

  • use var
  • execution is single-threaded

JS is built for the DOM

JS Bin

Functions are first-class objects

JS Bin

Semicolon insertion

function(){
    return
    1 + 2
}

function(){
    return;
    1 + 2;
}

➙ always use ;

Block-less JS

if (true)
 doThis()
 doThat()

if (true) {
 doThis();
}
doThat();

Data types

var num = 42;                               
var num2 = x * 42;                           
var str = "BioJS";                      
var arr = ["BioJS", "is", "aweseome"];           
var dict  = {name:"BioJS", url:"www.biojs.net"};

Access

  • arr[index]
  • dict.key or dict[key]

JSON

JavaScript Object Notation

  • JSON.parse(obj)
  • JSON.stringify(obj)
"course": {
    "chair": "RostLab",
    teachers: ["Tatyana Goldberg", "Juan Miguel Cejuela"],
    "location": {
        "postcode": "85748",
        "city": "Garching"
    }
}

Javascript type table

Value               Class      Type
-------------------------------------
"foo"               String     string
new String("foo")   String     object
1.2                 Number     number
new Number(1.2)     Number     object
true                Boolean    boolean
new Boolean(true)   Boolean    object
new Date()          Date       object
new Error()         Error      object
[1,2,3]             Array      object
new Array(1, 2, 3)  Array      object
new Function("")    Function   function
/abc/g              RegExp     object (function in Nitro/V8)
new RegExp("meow")  RegExp     object (function in Nitro/V8)
{}                  Object     object
new Object()        Object     object

Type coersion

'' == '0'          // false
0 == ''            // true
0 == '0'           // true

false == 'false'   // false
false == '0'       // true
true == '1'        // true

false == undefined // false
false == null      // false
null == undefined  // true

' \t\r\n ' == 0    // true
➙ always use ===

== is weakly-typed and slower than ===

Language rules

  • always
    • var
    • ;
  • no
    • eval,with
    • modification of builtin Objects (Object.prototype)
  • use sparingly
    • for-in (only for map/hash/associative arrays)
    • this (constructor, methods assignment)

Style tips

  • C++/Java rules
    • functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis
    • function () {
  • name your anounymous functions ➙ debugging
  • avoid new Object (➙ {}) && new Array(➙ [])

style guides

BE CONSISTENT

Iterators

Simple for loop

for(var i = 0; i < arr.length; i++){
    console.log(arr[i]);
}

forEach

arr.forEach(function(entry){
    console.log(entry);
});

for-in

for(var key in arr){
    console.log(arr[key]);
}

Attention:

for-in

  • slow
  • enumerates all the properties on the prototype chain
  • only use it for dicts
if (arr.hasOwnProperty(key){

}

Iterators

JS Bin

Other control constructs

JS Bin

Closures

JS Bin

Scope

JS Bin

Scope tricks

  • bind the scope
method.bind(instance);

CS / ES6: =>

  • save the scope
function(){
    var self = this;
    foo.on("click", function(evt){
        console.log(this,self);
    }
}

Hoisting

x = 5; // Assign 5 to x

elem = document.getElementById("demo"); // Find an element 
elem.innerHTML = x;                     // Display x in the element

var x; // Declare x

(only for var declarations)

Prototyping

JS Bin

Mixins

JS Bin
JS Bin

Events

  • trickling = capturing: root -> leaf
  • bubbling: leaf -> root

© yuiblog.com