15. var mrString = "All work and
no play makes Mr. String a
dull boy"
Donnerstag, 19. November 2009
16. "All work and no play makes Mr.
String a dull boy".redrum();
Donnerstag, 19. November 2009
17. "All work and no play makes Mr.
String a dull boy".redrum();
// -足-足> All work and no play makes Mr.
String a dull boy All work and no play
makes Mr. String a dull boy All work and no
play makes Mr. String a dull boy All work
and no play makes Mr. String a dull boy All
work and no play makes Mr. String a dull
boy
Donnerstag, 19. November 2009
18. "All work and no play makes Mr.
String a dull boy".redrum();
// -足-足> All work and no play makes Mr.
String a dull boy All work and no play
makes Mr. String a dull boy All work and no
play makes Mr. String a dull boy All work
and no play makes Mr. String a dull boy All
work and no play makes Mr. String a dull
boy
Donnerstag, 19. November 2009
20. All work and no play makes Mr. String a
dull boy All work and no play makes Mr.
String a dull boy All work and no play
makes Mr. String a dull boy All work and no
play makes Mr. String a dull boy All work
and no play makes Mr. String a dull boy
All work and no play makes Mr. String a
dull boy All work and no play makes Mr.
String a dull boy All work and no play
makes Mr. String a dull boy All work and no
play makes Mr. String a dull boy All work
and no play makes Mr. String a dull boy All
work and no play makes Mr. String a dull
boy All work and no play makes Mr. String a
dull boy All work and no play makes Mr.
String a dull boy All work and no play
makes Mr. String a dull boy All work and no
play makes Mr. String a dull boy
Donnerstag, 19. November 2009
21. "All work and no play makes Mr. String a dull
boy".redrum();
String.prototype.redrum = function(){
var i = 5, str ='';
while (i-足-足) { str += this +' '; }
return str;
}
Donnerstag, 19. November 2009
24. String.prototype.redrum = function(){
var i = 5, str ='';
while (i-足-足) { str += this +' '; }
return str;
}
// -足-足> All work and no play makes Mr. String a
dull boy All work and no play makes Mr. String
a dull boy All work and no play makes Mr.
String a dull boy All work and no play makes
Mr. String a dull boy All work and no play
makes Mr. String a dull boy
Donnerstag, 19. November 2009
28. returns a new array
Array#map
['Austria','Europe','World'].map(function(location){
return 'Hello '+location+'!';
}).sortBy(Math.random).invoke('toUpperCase').join(' ');
"HELLO WORLD! HELLO AUSTRIA! HELLO EUROPE!"
Donnerstag, 19. November 2009
29. Math.random is a
standard
JavaScript
functions returns
number [0..1]
['Austria','Europe','World'].map(function(location){
return 'Hello '+location+'!';
}).sortBy(Math.random).invoke('toUpperCase').join(' ');
"HELLO WORLD! HELLO AUSTRIA! HELLO EUROPE!"
Array#sortBy
sorts by the result
order given by
calling a function
reference
Donnerstag, 19. November 2009
30. ['Austria','Europe','World'].map(function(location){
return 'Hello '+location+'!';
}).sortBy(Math.random).invoke('toUpperCase').join(' ');
"HELLO WORLD! HELLO AUSTRIA! HELLO EUROPE!"
Array#invoke
call a method on
each item
Donnerstag, 19. November 2009
31. ['Austria','Europe','World'].map(function(location){
return 'Hello '+location+'!';
}).sortBy(Math.random).invoke('toUpperCase').join(' ');
"HELLO WORLD! HELLO AUSTRIA! HELLO EUROPE!"
Array#join
join items in a
string (standard
JavaScript)
Donnerstag, 19. November 2009
32. ['Austria','Europe','World'].map(function(location){
return 'Hello '+location+'!';
}).sortBy(Math.random).invoke('toUpperCase').join(' ');
No need for loop variables
Much higher readability than
classic statement-based
iteration
Object-oriented and extensible
Shorter code (chaining)
Donnerstag, 19. November 2009
33. Why Frameworks?
DOM API is clunky
Cross-browser differences
Utility functions
Dont repeat yourself
Donnerstag, 19. November 2009
36. // DOM API
document.getElementById('some_element').style.display = 'none';
Donnerstag, 19. November 2009
37. // DOM API
document.getElementById('some_element').style.display = 'none';
// Prototype
$('some_element').hide();
Donnerstag, 19. November 2009
38. hide all p elements that have a
important CSS class
$$('p.important').invoke('hide');
CSS selector
Donnerstag, 19. November 2009
39. function luhn_check(number) { verifyLuhn10: function(number){
var number=number.replace(/D/g, ''); return ($A(CreditCard.strip(number)).reverse().inje
var number_length=number.length; return a + $A((parseInt(n) * [1,2][index%2]).toSt
var parity=number_length % 2; .inject(0, function(b,o){ return b + parseInt(o)
}
var total=0;
for (i=0; i < number_length; i++) {
var digit=number.charAt(i);
if (i % 2 == parity) {
digit=digit * 2;
if (digit > 9) {
digit=digit -足 9;
same with
} Prototype
}
total = total + parseInt(digit);
}
if (total % 10 == 0) { pure JavaScript
return true;
} else {
implementation
return false;
}
}
Donnerstag, 19. November 2009
40. new Test.Unit.Runner({
testClassCreate: function() {
this.assert(Object.isFunction(Animal), 'Animal is not a constructor');
this.assertEnumEqual([Cat, Mouse, Dog, Ox], Animal.subclasses);
Animal.subclasses.each(function(subclass) {
this.assertEqual(Animal, subclass.superclass);
}, this);
var Bird = Class.create(Animal);
this.assertEqual(Bird, Animal.subclasses.last());
this.assertEnumEqual(Object.keys(new Animal).sort(),
Object.keys(new Bird).without('constructor').sort());
},
testClassInstantiation: function() {
var pet = new Animal("Nibbles");
this.assertEqual("Nibbles", pet.name, "property not initialized");
this.assertEqual('Nibbles: Hi!', pet.say('Hi!'));
this.assertEqual(Animal, pet.constructor, "bad constructor reference");
this.assertUndefined(pet.superclass);
var Empty = Class.create();
this.assert('object', typeof new Empty);
},
Donnerstag, 19. November 2009
41. /**
* Element#morph(@element, style[, options]) -足> element
*
* Dynamically adjust an element's CSS styles to the CSS properties given
* in the `style` argument.
*
* This method is the preferred way to invoke CSS-足based effects:
*
* $('element_id').morph('width:500px');
* $('element_id').morph('width:500px', 'slow');
* $('element_id').morph('width:500px', function(){ alert('finished!'); });
* $('element_id').morph('width:500px', 2); // duration 2 seconds
*
* Complex options can be specified with an Object literal:
*
* $('element_id').morph('width:500px;height:500px', {
* duration: 4,
* transition: 'linear',
* delay: .5,
* propertyTransitions: {
* width: 'mirror', height: 'easeInOutCirc'
* },
* after: function(){ alert('finished'); }
* });
*
* Morphs can be chained:
*
* // the height morph will be executed immediately following
* // the width morph
* $('element_id').morph('width:500px').morph('height:500px');
*
* By default, `morph` will create a new [[S2.FX.Queue]] for the
* element if there isn't on already, and use this queue for queueing
* up chained morphs. To prevent a morph from queuing at the end,
Donnerstag, 19. November 2009
42. .
prototype
.
Language extensions
DOM manipulation
Syntactic sugar
Reduce code size/LOC
Infrastructure for JavaScript apps
Donnerstag, 19. November 2009