JavaScript notes


jQuery demo
http://renownedmedia.com/examples/nexus/

jQuery snake game
http://jquery-snakey.googlecode.com/svn/trunk/index.html


this keyword and listeners
http://www.quirksmode.org/js/this.html


Objects (from Eloquent JavaScript Ch. 4)

// empty object
var empty = {}; 

remove properties with "delete obj.property"

Iterate through object properties:

for (var cat in livingCats)
  print(cat);

Placeholder and object creation

function catRecord(name, birthdate, mother) {
  return {name: name, birth: birthdate, mother: mother};
}

To compare Dates, use .getTime() method

Functional Programming (Chapter 6)

function forEach(array, action) {
  for(var i = 0; i < array.length; i++) 
    action(array[i]);
}

function sum(numbers) {
  var total = 0;
  forEach(numbers, 
          function (number) {
            total += number;
          });
  return total;
}

function negate(func) {
  return function(x) {
    return !func(x);
  };
}

function reduce(combine, base, array) {
  forEach(array, 
          function (element) {
            base = combine(base, element);
          });
  return base;
}

function map(func, array) {
  var result = [];
  forEach(array,
          function (element) {
            result.push(func(element));
          });
  return result;
}
Concat creates a new array, while push does not

No comments:

Post a Comment