Nice book written by Scott Duffy.
Few quotations i wanted to share from this book from the next 4 chapters(4-5-6-7)
1) Create an Array Object
There are three ways to create an Array using the JavaScript Array class:
new Array ()
new Array (size)
new Array (element1, element2, ..., elementN)
2) Few important Array methods
| Method | Description |
|---|---|
| concat (array1, array2...arrayN) | Joins two or more arrays into a single array |
| join (separator) | Joins all the elements of an array into a single string, separated by the separator indicated or a comma if none is specified |
| pop() | Returns the last element of an array and removes it |
| push (element1, element2...elementN) | Adds one or more elements to the end of an array and returns its new length |
| reverse () | Reverses the order of the elements in an array |
| shift () | Similar to pop (); returns the first element of an array and removes it |
| slice (begin, end) | Creates a new array from a subsection of the existing array,as defined by the start and end indexes |
| splice (index, howMany, element1,element2...elementN) | Used to add or remove elements from an array |
| sort (functionName) | Used to sort the elements of an array |
| unshift (element1, element2...elementN) | Similar to push (); adds one or more elements to the beginning of an array and returns its new length |
3) Create Objects in JavaScript 1.x
There are four ways to create your own objects in JavaScript 1.x:
a) Call a constructor function
b) Use an object literal
c) Extend an existing class
d) Extend an existing object
4) Call a Constructor Function
One way to create an object in JavaScript is to create a special function called an object constructor. It is the job of this constructor to initialize the object for use—essentially to perform all the necessary setup tasks. The constructor function starts off looking just like a regular function, but uses a special object—the this object—to add properties to itself.
Example:
<script>
function Car (make, model, year, color) {
this.Make = make;
this.Model = model;
this.Year = year;
this.Color = color;
this.FullName = this.Year + " " +
"<b>" + this.Make + "</b> " +
this.Model;
}
var mySUV = new Car("Toyota", "4Runner SR5",2001, "Thundercloud");
document.write ("I drive a " + mySUV.FullName);
</script>
5) Use an Object Literal
The JavaScript object literal is a convenient way to create an object without having to predefine a constructor function. It is, in effect, an unnamed constructor. One of JavaScript’s greatest strengths is that it allows programmers to create programs quickly, and the object literal is a prime example of how it does this. The tradeoff to creating objects quickly using the object literal is that you lose out on the ability to reuse code for other objects or other programs.
var mySUV = {Make:"Toyota",Model:"4Runner SR5",Year:2001,Color:"Thundercloud"};
6) Extend an Existing Class
JavaScript allows you to extend the capabilities of a class by assigning your own methods and properties to it. This includes adding new methods and properties to any of the predefined system classes. It does this through a system called prototyping. JavaScript automatically gives each class a property called prototype that can be used to extend the functionality at the class level.
ClassName.prototype.member1 = value1;
Example:
<script>
function getSeason () {
var mon = this.getMonth();
var day = this.getDay();
if (mon < 2 || (mon == 2 && day < 20)) {
// Covers Jan 1 through Mar 19
return "Winter";
} else if (mon < 5 || (mon == 5 && day < 21)) {
// Covers Mar 20 through June 20
return "Spring";
} else if (mon < 8 || (mon == 8 && day < 23)) {
// Covers June 21 through Sept 22
return "Summer";
} else if (mon < 11 || (mon == 11 && day < 22)) {
// Covers Sept 23 through Dec 21
return "Fall";
} else {
// Covers Dec 22 through Dec 31
return "Winter";
}
}
Date.prototype.getSeason = getSeason;
var today = new Date();
document.write("The season is currently " + today.getSeason());
</script>
7) Extend an Existing Object
Just as a class can be extended using the prototype property, objects themselves can have new
properties and methods attached to them.
<script>
function Car (make, model, year, color) {
this.Make = make;
this.Model = model;
this.Year = year;
this.Color = color;
this.FullName = this.Year + " " +
"<b>" + this.Make + "</b> " +
this.Model;
}
var mySUV = new Car("Toyota", "4Runner SR5",2001, "Thundercloud");
mySUV.mileage = 12323;
document.write(mySUV.FullName + " has traveled " + mySUV.mileage + " miles.<br><br>");
</script>
About the Author
Scott Duffy has been providing IT consulting services to medium- and large-sized businesses and government organizations for more than six years. Before embarking on a career as a consultant, Scott worked at two of the largest corporations in Canada as a software developer.His 12 years of professional experience cover a wide range of platforms and technologies, including programming in mainframe, client-server, and web-based application environments. He is actively involved in every stage of the software development process, including team
management.
When he’s not designing software applications for clients, Scott keeps himself busy with his writing projects. He is currently working on his next book for McGraw-Hill/Osborne, a study guide for the Microsoft MCSD 70-300 exam.
To contact Scott to discuss your organization’s business needs, or about any other matter,please e-mail him at scott.duffy@mydemos.com or visit his web site at http://www.mydemos.com.
Hope you enjoy reading this book.
No comments:
Post a Comment