Asynchronous Javascript And XML
var api = 'https://api.github.com/users/xudafeng';
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', api, true);
xmlHttp.onreadystatechange = function(d) {
if (xmlHttp.readyState == 4) {
console.log(xmlHttp.responseText);
}
};
xmlHttp.send(null);
var api = 'https://api.github.com/users/xudafeng';
$.ajax({
method: 'GET',
url: api,
data: {
foo: 'bar'
}
}).done(function(msg) {
console.log(msg);
});
JavaScript Object Notation
JSON with Padding
var bar = function() {
console.log('bar');
};
bar();
var obj = {
foo: function() {
console.log('foo');
}
};
obj.foo();
start with closure
(function() {
console.log(this)
})()
module.exports = function() {
console.log('foo');
}
exports.foo = function() {
console.log('bar');
}
define(function(require, exports, module) {
var foo = require('foo');
var bar = require('bar');
console.log(foo);
});
publish/subscribe
var EventEmitter = require('events').EventEmitter;
var foo = new EventEmitter();
foo.on('bar', function(d) {
console.log(d)
});
foo.emit('bar', {
data: 'data'
});
function() {
// right
var foo = 'bar';
// wrong
var foo = 'bar';
}
// right
var foo = 'bar';
// wrong
var foo = 'bar'
// right
var foo = 'bar';
// wrong
var foo = "bar";
// right
if (true) {
console.log('winning');
}
// wrong
if (true)
{
console.log('losing');
}
// right
var keys = ['foo', 'bar'];
var values = [23, 42];
// wrong
var keys = ['foo', 'bar'],
values = [23, 42];
===
operator
// right
if (foo !== 1) {
console.log('bar');
}
//wrong
if (foo != 1) {
console.log('bar');
}