<html>
<head>
<script type="text/javascript"
src="my_scripts.js"></script>
</head>
<body></body>
</html>
doStuff();
function doStuff(arg1, arg2) {}
eatIt();
var eatIt = function(){}
var module = {
eatIt: function(){
//arguements
//this == module
}
};
module.eatIt("chicken", 3);
/* */
var Person = function(name){
this.name = name;
};
var dude = new Person('Sean');
if (1===1) {
var name = 'Sean';
}
//Same scope; redefines name
var name = 'Sean';
/* */
var name = 'Sean';
var action = function(){
//Closures!
var fullName = name + ' Massa';
return fullName;
}
//Different scope!
typeof fullName === 'undefined';
setTimeout(function(){
console.log('anonymous function');
}, 1000);
function timeoutHandler(){
console.log('named function');
}
setTimeout(timeoutHandler, 1000);
function myTimeout(callback, timeout) {
callback("some param");
}
var Person = function(){
this.speak = function(msg){
console.log(msg);
};
}
/* */
var EfficientPerson = function(){};
EfficientPerson.prototype.speak =
function(msg) {
console.log(msg);
};
var Person = function(){};
Person.prototype.speak = function(){...};
var Employee = function(){};
Employee.prototype = new Person();
var emp = new Employee();
emp.speak();
//gather data, do stuff
jQuery === $
$.noConflict()
jQuery !== $
var $cards = $('.card');
var card = $cards[0];
$cards.hide();
$(function(){
//onload, do stuff!
});
var $container = $('#container')
var $newCard = $(
'some card');
$container.append($newCard);
(function( $ ){
$.fn.myPlugin = function() {
//this == a jQuery object
this.fadeIn('normal', function(){
// this == a DOM element
});
};
})( jQuery );
$('#element').myPlugin();
function removeMyself(){
$(this).remove();
}
$('.card').bind('click', removeMyself);
$('input.select').bind('change', function(){
recalculateTotals();
});
$('.card').live('click', removeMyself);
if ( target.addEventListener )
target.addEventListener(
eventName,
handlerName,
false);
else if ( target.attachEvent )
target.attachEvent(
"on" + eventName,
handlerName);
else
target["on" + eventName] = handlerName;
//jQuery will save you!
$(selector).bind(eventName, handler);
//newer syntax
$(selector).on(eventName, handler);
# Assignment:
number = 42
opposite = true
# Conditions:
number = -42 if opposite
# Functions:
square = (x) -> x * x
# Arrays:
list = [1, 2, 3, 4, 5]
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
# Splats: race = (winner, runners...) -> print winner, runners # Existence: alert "I knew it!" if elvis? # Array comprehensions: cubes = (math.cube num for num in list)
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200,
{'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log(
'Running at http://localhost:1337/');
/