Today i started reading Practical Prototype and script.aculo.us By Andrew Dupont.
Nice book written by Andrew Dupont.
I want to share few quotations i found from this book from the first 2 chapters.
1) Everything in JavaScript is an object.
2) All Data Types Have Instance Methods
Like other languages that embrace object orientation, every object can have instance methods. This allows for flexible syntax and makes code easier to read.
["foo", "bar", "baz"].join(' ');
//=> "foo bar baz"
"foo bar baz".split(' ');
//-> ["foo", "bar", "baz"]
It also ensures that functions don’t clutter up the global namespace. What’s the use of a generic join function, for instance, if it works only on arrays? Or a generic split that works only on strings?
3) All Data Types Inherit from Object
JavaScript boasts half a dozen native data types: Object, String, Array, RegExp (for regular expressions), Boolean (true or false), and Date.
I place Object first because it’s the root data type. When I say that everything in JavaScript is an object,
Object can be thought of as a blank data type, the empty canvas that all other types start with. There is nothing Object does that another type can’t do, but then that’s the point: Objects can bend to your will.
But back to the main point: Everything in JavaScript is an Object.We can verify this with the instanceof operator on some core JavaScript data types:
Array instanceof Object;
//-> true
RegExp instanceof Object;
//-> true
Date instanceof Object;
//-> true
String instanceof Object;
//-> true
Function instanceof Object;
//-> true
4) All Objects Have Prototypes
It’s quite simple to add instance methods to arrays, strings, or any other native types:
Array.prototype.double = function() {
var newArray = [];
// inside this function, "this" refers to the array
for (var i = 0, length = this.length; i < length; i++) {
newArray.push(this[i]);
newArray.push(this[i]);
}
return newArray;
};
var exampleArray = new Array(1, 2, 3);
// inherits all the properties of Array.prototype
exampleArray.double();
//-> [1, 1, 2, 2, 3, 3]
5) Prototype is a JavaScript library that aims to ease development of dynamic web applications. Its development is driven heavily by the Ruby on Rails framework, but it can be used in any environment.
6) The $ Function
DOM methods have intentionally verbose names for clarity’s sake, but all that typing gets tiresome very quickly. Prototype’s solution is simple but powerful: it aliases the oftused
DOM method document.getElementById to a function simply named $.
For example, instead of
document.getElementById('menu'); //->
with Prototype, you can write
$('menu'); //->
Like document.getElementById, $ finds the element on the page that has the given id attribute.
7) Prototype gives us Object.extend. It takes two arguments, destination and source,and both objects, and loops through all the properties of source, copying them over to destination. If the two objects have a property with the same name, then the one on destination takes precedence.
Object.extend also solves our typing woes when extending built-ins:
Object.extend(String.prototype, {
strip: function() {
// ...
},
gsub: function() {
// ...
},
times: function() {
// ...
},
toQueryParams: function() {
// ...
}
});
8) • Querying by attribute:
• $('input[type="text"]') will select all text boxes.
• $$('a[rel]') will select all anchor tags with a rel attribute.
• $$('a[rel~=external]) will select all a elements with the word "external" in the rel attribute.
About the Author
ANDREW DUPONT is a UI developer living and working in Austin, Texas. He is a member of the core development team for Prototype, the popular JavaScript toolkit. He has contributed to Prototype in many different ways: writing code and documentation, offering support, and evangelizing to colleagues. In addition, Andrew has spoken about JavaScript at South by Southwest Interactive and other tech industry conferences.
Andrew received liberal arts and journalism degrees from the University of Texas at Austin. He occasionally attended classes, but much preferred the time between classes, during which he experimented with web design and learned about the emerging web standards movement.
Monday, 24 November 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment