Monday, 15 September 2008

How to do everything in Javascript - Part1

Today i started reading How to do everything in Javascript by Scott Duffy.

Nice book written by Scott Duffy.

Few quotations i wanted to share from this book from the 1st 3 chapters.

1) Netscape introduced JavaScript with great fanfare in December 1995 as an “open, cross-platform object scripting language.” It was billed as the perfect complement to the much-anticipated Java programming language, since the two languages can communicate with each other inside a browser window.

2) JavaScript Makes Its Way into Internet Explorer
Soon after Netscape Communications first introduced JavaScript in its Navigator 2.0 browser,Microsoft realized the importance of incorporating this language into its Internet Explorer browser.Since Netscape was not exactly going to mail Microsoft the source code, and even the language specification was a well-guarded secret, Microsoft was forced to reverse engineer JavaScript to create its own version. Microsoft named its version JScript, since Netscape owned the trademark on the word JavaScript.

Early versions of JScript did not perform certain functions in exactly the same way JavaScript did, and so JavaScript incompatibility between the browsers was something developers often had to take into account when scripting their web pages.

3) A compiler is a program that turns programming code into machine-ready form, often called a binary or an executable.

4) Display an Alert Message
JavaScript provides three types of pop-up dialog boxes for use in your applications:

An alert box
A confirm box
A user-input prompt box

5) The for-in Loop
The for-in loop will extract each tool individually from that tool chest, and allow you to examine and manipulate it on its own.

Example:


<html>
<head>
<title>JavaScript Test</title>
</head>
<body>
<h1>for-in Loop</h1>
<script language="JavaScript" type="text/javascript">
<!-- // Begin
// We define the tool chest object
var toolchest = {tool1:"Wrench",
tool2:"Hammer",
tool3:"Cordless drill",
tool4:"Needlenose pliers"};
// for-in iterates over its properties
for (var tool in toolchest) {
document.write(toolchest[tool] + "<BR>");
}
// End -->

</script>
</body>
</html>


6) The with Statement

The with statement allows you to access the methods and properties of an object without having to specify the name of the object on every line.Without the with statement, you will always need to specify the name of the object you are working on next to every property or method you wish to access:


person.fullname = "Bob Jones";
person.haircolor = "Black";
person.eat();
person.sleep();



But when statements are surrounded by the with statement, the object name is implied, as in:


with (person) {
fullname = "Bob Jones";
haircolor = "Black";
eat();
sleep();
}



7) JavaScript’s Built-in Classes and Data Types

Below are JavaScript's Built-in Classes and Data Types.







Class NameDescription
ArrayAllows lists of data to be stored in a single variable
BooleanA wrapper for Boolean values
DateLets you work with dates and times
FunctionAllows you to define a function programmatically
MathContains convenient mathematical constants and functions/tr>
NumberA wrapper for primitive numeric values/tr>




8) Instance Members Versus Static Members

By default, properties and methods of a class will be defined as instance members. This means that they can only be accessed within the instance of an object based on that class. These objects are created using the new keyword, and a new copy of the class’s instance members will be created for each new object created. If you create 100 objects based on a class, you have 100 separate copies of each method and variable. Changing the value of one of the copies does not affect the others.

Properties and methods of a class can optionally be defined as static members. Static members can only be accessed directly from the class and are not part of instances created based on that class. In essence, static class members are global, since only one version of that member ever exists, regardless of how many objects have been created from it.

9) Understand the Basics of Regular Expressions

Regular expressions in JavaScript deal with pattern matching. Other programming languages allow regular expressions to search and replace text, but JavaScript programmers do not have that luxury. Despite this, regular expressions are still a powerful way to search for complex patterns.

For instance, you can use them to

Validate telephone numbers in more than one format
Extract all the numbers from a string
Ensure a date is entered in the valid format
Verify an e-mail address has the proper format

As we saw in the preceding code example, the regular expression pattern is quite complex and can be a little bit intimidating.

var pattern = /^\(\d{3}\) \d{3}-\d{4}$/;

As I mentioned, regular expressions have their own language. There are many, many symbols and special characters to represent different things. The following are some general rules:

a. We start with defining a variable for our regular expression pattern.
var pattern;

b. The forward slash (/ ) signifies the start and end of a regular expression literal in JavaScript. Regular expression literals allow us to create a regular expression object without having to use the RegExp class.
var pattern = //;

c. The caret (^) and dollar sign ($) represent the start and end of a line. They also represent the start and end of a string. If we only wanted to match the pattern to part of the string,we would omit them. Similarly, if we wanted to test specifically for a pattern at the beginning of a long string, we could just use the caret (^) by itself. The following pattern only matches an empty string (“”) so far.

var pattern = /^$/;

d. The \d represents a single digit. If we add the \d to our pattern, it would only match a one-character string containing a number from 0 to 9.

var pattern = /^\d$/;

e. If we want to match more than one number (as we do with a phone number), we can put multiple \d tokens together; \d\d\d would represent three numbers. We can also specify the number in curly braces: \d{3}.

var pattern = /^\d{3}$/;

f. Other characters in the pattern, such as spaces and hyphens, represent only themselves.So the following pattern would match 999-999-9999, which is one way to represent a telephone number.

var pattern = /^\d{3}-\d{3}-\d{4}$/;

g. Since the parentheses characters have special meaning in regular expression syntax,to include them we will need to escape them with a backslash: \( and \). That gives us a pattern that matches (999) 999-9999, which is what we want.

var pattern = /^\(\d{3}\) \d{3}-\d{4}$/;


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.

1 comment:

Anonymous said...

top [url=http://www.001casino.com/]001casino.com[/url] brake the latest [url=http://www.casinolasvegass.com/]casino las vegas[/url] unshackled no deposit hand-out at the best [url=http://www.baywatchcasino.com/]baywatch casino
[/url].