Awesome things about
Javascript
First Class Functions
Closures
Scope
Context
Event Driven Environment
First Class Functions
Functions can be passed as arguments.
function foo(v1,v2,callback){
console.log(v1,v2);
callback();
}
foo("one","two", function(){
console.log("three");
});
////Output:
//one two
//three
Event Driven Environment
Javascript stays in memory waiting for events.
$(document).ready(function(){
console.log("Hello");
$('button').on('click', function(){
console.log("World!");
});
});
////Output:
//Hello
//World!
//World!
//World!
//...
Closures
Retains state and scope after code executes.
$(document).ready(function(){
var foo = 1;
$('button').on('click', function(){
foo++;
console.log(foo);
});
});
////Output:
//2
//3
//...
Scope
Scope refers to what variables are available for access.
var a = 1;
function foo(){
var b = 2;
console.log(a,b);
}
console.log(a,b);
////Output:
//1 2
//>>Reference Error: b is undefined
var a = 1;
function foo(){
var a = 1;
a = 3;
console.log(a);
}
console.log(a);
////Output:
//3
//1
var a = 1;
function foo(){
b = 2;
console.log(a, b);
}
console.log(a, b);
////Output:
//1 2
//1 2
Context
Context is what the value of is on the running code.
this
function foo(){
console.log(this === window);
}
window.foo();
////Output:
//true
var obj = {
foo: function(){
console.log(this === window,this === obj);
}
};
obj.foo();
///Output:
//false true
var obj = {
foo: function(){
console.log(this === window,this === obj);
}
};
obj.foo.call(window);
//obj.foo.call(window, 1, 2, .. , n);
//obj.foo.apply(window, [1, 2, .. , n]);
///Output:
//true false
var obj = {
foo: function(){
console.log(this === window,this === obj);
}
};
var boundFunction = obj.foo.bind(window);
boundFunction();
///Output:
//true false
awesome.js
awesome js
Javascript is Awesome
By Daniel Contreras
Javascript is Awesome
- 485