Developed JavaScript in 1995
var
is not optional. Use it.;
can be optional. Use it.===
and !==
over ==
and !=
var x = 1;
var y = "oh yeah!";
eval("alert('meh');");
// JavaScript
function Dog() {
this.bark = function() {
return "WHOOF!"
};
}
var rex = new Dog();
rex.bark();
// Java
public class Dog() {
public String bark() {
return "WHOOF!"
};
}
Dog rex = new Dog();
rex.bark();
function Animal() {
this.breath() {
console.log("I'm breathing");
}
}
function Dog() {
this.bark() {
console.log("WHOOOF!");
}
}
function Fish() {
this.swim() {
console.log("GLOOB GLOOB!");
}
}
Dog.prototype = new Animal();
Fish.prototype = new Animal();
function Bubbles() {
this.bubble() {
console.log("Making bubbles");
}
}
Object.prototype.includes = function (constructor) {
var obj = new constructor();
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
this.prototype[prop] = obj[prop];
}
}
};
Fish.includes(Bubbles);
var fish = new Fish();
fish.bubble();
JS is Lisp in C's Clothing(Douglas Crockford)
; SCHEME
(define calculate (lambda (op v1 v2)
(op v1 v2)))
(define sum (lambda (a b) (+ a b)))
(define mult (lambda (x y) (* x y)))
(calculate sum 2 3) ; retorna 5
(calculate mult 2 3) ; retorna 6
// JavaScript
function calculate(op, v1, v2) {
return op(v1, v2);
}
function sum(a, b){
return a + b;
}
function mult(x, y){
return x * y;
}
calculate(sum, 2, 3); // retorna 5
calculate(mult, 2, 3); // retorna 6
$(function(){
console.log("DOM loaded")
});
$(".clickable").click(function(){
console.log(this + " was clicked")
});
function hey(text, name) {
console.log(text + ", " + name);
}
hey("Bom dia", "João");
hey("Bom dia", "José");
hey("Bom dia", "Nicolau");
function hey(text) {
return function(name) {
console.log(text + ", " + name);
}
}
var bomDia = hey("Bom dia");
bomDia("João");
bomDia("José");
bomDia("Nicolau");
try{
cause_an_error();
} catch(err) {
console.log("Ooops: ", err.description);
};