document.write('Hello World!');
var Book = function(price, author){
var _price = price;
var _author = author;
this.getAuthor = function(){
return _author;
}
};
var myBook = new Book(80, 'Douglas Crockford');
myBook.getAuthor(); // outputs 'Douglas Crockford'
Self-executing functions
(
function(name) {
alert('Hello ' + name);
}
)('John Doe')
Inner functions
function Person() {
function getName() {
return 'John Doe';
}
alert('Hello ' + getName());
}
alert('Hello ' + getName());
Self-rewriting functions
var sayName = function() {
alert('John Doe');
sayName = function() {
alert('Already did that');
}
}
var Person = function(firstName, lastName) {
var _firstName = firstName;
var _lastName = lastName;
this.getFirstName = function() {
return _firstName;
}
this.getLastName = function() {
return _lastName;
}
}
Person.prototype.getFullName = function() {
return this.getFirstName() + ' ' + this.getLastName();
}
var x = 12;
function getX() {
console.log(x);
var x = 3;
console.log(x);
}
getX();
// output:
// 'undefined'
// 3
Why?
RequireJS / CommonJS
define(function(require, exports)){
'use strict';
var Logger = require('util/Logger'),
PubSub = require('core/PubSub'),
MyModule = require('application/modules/MyModule');
var module = new MyModule();
module.start();
exports.getResult = module.processData();
}
Less / Sass
@bgColor: red;
@borderColor: black;
@alternateBgColor: green;
.decorator() {
background-color: @bgColor;
border: 1px solid @borderColor;
}
.myClass {
.decorator;
.myInnerClass {
background-color: @alternateBgColor;
}
}
A package manager for the web
Jasmine / Mocha / Chai
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});