這是一本JavaScript的入門書籍。作者用通俗易懂的語(yǔ)言講述JavaScript的基本內(nèi)容,由書名就能看得出來(lái),讀者只需要一個(gè)周末就能將它看完。書的最后兩章順帶介紹了微軟的JScript和WSH。可以在http://www.cnshare.org 上下載。
星期五晚上:"Introducing JavaScript and JScript."
介紹了JavaScript和JScript的歷史知識(shí)和基本情況。
星期六早晨:"Learning the Basics of JavaScript Coding."
講解JavaScript的基本語(yǔ)法、函數(shù)、數(shù)組,以及基于對(duì)象的特性。
星期六中午:"Using JavaScript to Build Better Web Pages."
詳細(xì)描述了JavaScript對(duì)象的特性。首先介紹了Browser里的document、form、location等特殊類,然后介紹如何編寫自己的對(duì)象,最后介紹如何處理Frames和表單。
星期六晚上:"Doing Really Cool Things with Your Web Pages."
介紹了一些特別的東西。比如如何控制狀態(tài)欄,如何控制瀏覽器,展示各種不同的彈出窗口,讓JavaScript工作在不同的瀏覽器中,用JavaScript制作的動(dòng)畫。
星期天早晨:"Advanced JavaScript Coding."
JavaScript高級(jí)編程。首先介紹的是cookie操作,然后是寫JavaScript的一些經(jīng)驗(yàn),最后用了很大的篇幅介紹一個(gè)在線書店的制作。
星期天中午: "Learning How to Use JScript and the WSH."
略。
星期天晚上: "Using JScript to Automate Windows Tasks."
略。
方法
for
…in循環(huán)
用來(lái)遍歷一個(gè)特殊對(duì)象的全部屬性。
for (variable in object) {
? statements;
}
每次循環(huán)這個(gè)object的一個(gè)屬性就被設(shè)為variable。
?
示例:
<HTML>
? <HEAD>
??? <TITLE>Script 2.18 - Demonstration of the for
…in statement
</TITLE>
? </HEAD>
? <BODY>
??? <SCRIPT LANGUAGE="JavaScript" TYPE="Text/JavaScript">
??? <!-- Start hiding JavaScript statements
?
????? for (i in navigator) {
????????? document.write(i,"<BR>");
????? }
?
??? // End hiding JavaScript statements -->
??? </SCRIPT>
? </BODY>
</HTML>
?
?
with
方法
這是一個(gè)簡(jiǎn)便的方法來(lái)執(zhí)行對(duì)某個(gè)object的操作。
with (object) {
? statements;
}
在括號(hào)里的語(yǔ)句可以執(zhí)行object的方法,而不用多敲"object."。例如,將object換為document,在statements中只要寫write(...),就相當(dāng)于document.write(...)。
?
示例:
<HTML>
? <HEAD>
??? <TITLE>Script 2.19 - Demonstration of the with statement
</TITLE>
? </HEAD>
? <BODY>
??? <SCRIPT LANGUAGE="JavaScript" TYPE="Text/JavaScript">
??? <!-- Start hiding JavaScript statements
?
????? with (document) {
??????? write("The with statement saved me a few keystrokes.
<BR>");
??????? write("Its use is purely discretionary!");
????? }
?
??? // End hiding JavaScript statements -->
??? </SCRIPT>
? </BODY>
</HTML>
當(dāng)然,這個(gè)方法除了省去你一些敲鍵盤的時(shí)間外,沒(méi)有其他的功能。
?
?
?
?
?
數(shù)組
?
排序數(shù)組
數(shù)組里有sort()方法來(lái)排序它的內(nèi)容,直接看示例:
<HEAD>
? <TITLE>Script 2.25 - Sorting an Array</TITLE>
</HEAD>
? <BODY>
??? <SCRIPT LANGUAGE="JavaScript" TYPE="Text/JavaScript">
??? <!-- Start hiding JavaScript statements
?
????? Animals = new Array("mice", "dog", "cat", "hamster", "fish");
?
????? document.write("<B>List of animals in the Zoo</B><BR>");
????? document.write(Animals.sort());? //
輸出cat,dog,fish,hamster,mice
?
??? // End hiding JavaScript statements -->
??? </SCRIPT>
? </BODY>
</HTML>
?
?
?
對(duì)象
?
Function
對(duì)象
JavaScript
同意你創(chuàng)建Function對(duì)象。對(duì)于一般的方法并不適用。這樣可以讓你更好的組織你的方法,但是由于Function對(duì)象固有的效率問(wèn)題,最好少用。
語(yǔ)法:FunctionName = new Function([p1,.......pn] body)
示例:Warn = new Function("alert('HI!')");
?
?
Math
對(duì)象
下面列出了Math對(duì)象的一些屬性和方法。
Table 2.5. SUMMARY OF MATH PROPERTIES
|
Property
|
Description
|
E
|
Euler's constant (2.718)
|
LN2
|
Natural logarithm of 2 (2.302)
|
LN10
|
Natural logarithm of 10 (.693)
|
LOG2E
|
Base 2 logarithm of e (.434)
|
LOG10
|
Base 10 logarithm of 10 (1.442)
|
PI
|
Ratio of the circumference of a circle to its diameter (3.141549)
|
SQRT1_2
|
Square root of ? (.707)
|
SQRT2
|
Square root of 2 (1.414)
|
?
Table 2.6. SUMMARY OF MATH METHODS
|
Method
|
Description
|
abs()
|
Returns the absolute value
|
cos(), sin(), tan()
|
Trigonometric functions
|
acos(), asin(), atan()
|
Inverse trigonometric functions
|
exp(), log()
|
Exponential and natural logarithms
|
ceil()
|
Returns the lowest integer greater than or equal to the argument
|
floor()
|
Returns the highest integer less than or equal to the argument
|
min(x,y)
|
Returns either x or y, depending on which is lower
|
max(x,y)
|
Returns either x or y, depending on which is higher
|
pow(x,y)
|
Returns the value of x y
|
random()
|
Returns a random number between 0 and 1
|
round()
|
Rounds the argument to the nearest integer
|
sqrt()
|
Returns the square of the argument
|
?
?
?
Object
對(duì)象
Object
對(duì)象是所有對(duì)象的基礎(chǔ)。
MyNumberObject = new Object(99.99);
MyStringObject = new Object("Testing");
?
?
?
String
對(duì)象
下面列出了String對(duì)象的常用方法。
Table 2.8. COMMLY USED STRING OBJECT METHODS
|
Method
|
Description
|
charAt()
|
Returns the character at the specified position in the string where the index of a String object begins at zero
|
concat()
|
Combines two strings into one new string
|
fromCharCode()
|
Creates a string value based on the supplied code set
|
indexOf()
|
Returns the position of a specified substring
|
lastIndexOf()
|
Returns the last position of a specified substring
|
slice()
|
Creates a new string using a portion of the current string
|
split()
|
Organizes a string into an array
|
substring()
|
Returns the specified portion of a string
|
toLowerCase()
|
Returns the string in all lowercase characters
|
toUppercase()
|
Returns the string in all uppercase characters
|
?
?
?
?
瀏覽器對(duì)象
window
對(duì)象
關(guān)于窗口的操作
window.alert()
window.close()
window.forward()
window.back()
window.home()
window.resizeTo(300,400)
等
?
?
document
對(duì)象
最常用的對(duì)象!
_????????
anchors[].
An array containing a list of all anchors in the document
_????????
applets[].
An array containing a list of all applets in the document
_????????
embeds[].
An array containing a list of all embedded objects in the document
_????????
forms[].
An array containing a list of all forms in the document
_????????
images[].
An array containing a list of all images in the document
_????????
links[].
An array containing a list of all links in the document
_????????
plugins[].
An array containing a list of all plug-ins in the document
Other document object properties enable you to affect appearance:
_????????
bgColor.
Specifies the document background color
_????????
fgColor.
Specifies the color of document text
_????????
linkColor.
Specifies the color of links
_????????
alinkColor.
Specifies the color of active links
_????????
vlinkColor.
Specifies the color of visited links
Here is a partial list of other useful document properties:
_????????
cookie.
Lets you get and set cookie values
_????????
lastModified.
A string that shows the date and time at which the document was last changed
_????????
referrer.
A string showing the URL the user came from
_????????
title.
A string containing the contents of the HTML <TITLE> tags
_????????
URL.
A string containing the document's URL
?
?
?
?
高效程序員的習(xí)慣
?
寫大量的注釋。
?
注意縮距讓你的代碼易讀。
?
盡量將代碼模塊化。
?
在腳本的頂端聲明方法和變量。
?
以一致的方式命名你的變量和方法。
?
用一致的語(yǔ)法命名變量和方法。
?
不要僅運(yùn)行了一次而認(rèn)為你的代碼沒(méi)有bug。
?
用不支持JavaScript的瀏覽器運(yùn)行你的代碼,讓你的腳本有一個(gè)正確的輸出。
?
測(cè)試所有可能的場(chǎng)景。
?
寫一點(diǎn)測(cè)試一點(diǎn)。
?
以瀏覽器不同的大小來(lái)測(cè)試你頁(yè)面的輸出,保證它們都是你想要的。
?
測(cè)試每個(gè)事件處理器。
?
用變量的關(guān)鍵字明確的聲明變量。
?
方法或變量名用描述性的語(yǔ)言,而不用單個(gè)字母。
?
取對(duì)象名時(shí)小心系統(tǒng)已經(jīng)定義的名字。
?
注意引號(hào)里的字符。
?
Remember to use plenty of comments.
Comments enable you to explain why you wrote the script the way you did and to explain particularly difficult sections of code. Comments make it much easier for others to follow behind you and for you to understand your own code months or years later.
?
Always use indentation to make your code easy to read.
Indenting statements also makes it easier for you to match up beginning and ending tags, curly braces, and other HTML and script elements.
?
Write modular code.
Whenever possible, group your statements into functions. Functions let you group related statements, and test and reuse portions of code with minimal effort. Simplify your design by assigning only one task to a function.
?
Declare functions and variables at the top of your scripts.
This approach makes those elements easy to find and modify, and is a lot less confusing than embedding them throughout different portions of lengthy scripts. This technique also helps to ensure that the functions and variables are defined before they are referenced.
?
Be consistent in the way you name variables and functions.
Try using names that are long enough to be meaningful and that describe the contents of the variable or the purpose of the function.
?
Use consistent syntax when naming variables and functions.
In other words, keep them all lowercase or all uppercase; if you prefer Camel-Back notation, use it consistently.
?
Do not assume that your script is bug free just because it ran once without an error.
Make sure that you test the script using different browsers and, if possible, with different versions of the same browser.
?
Make sure that you test your script using browsers that do not support JavaScript to ensure that your script properly provides alternative content.
Try disabling support in your browsers for things such as frames to make sure that all browsers display your information as you intend it to be displayed.
?
Test all possible scenarios.
This includes testing with good and bad data. If your pages have forms, enter invalid data and check to make sure that the validation routines work as you think they will. Test every link and click on every button.
?
Test long scripts in a modular fashion.
In other words, do not try to write the entire script before testing any portion of it. Write a piece and get it to work before adding the next portion of code.
?
Load your pages using different resolutions to make sure that they look as you expect them to at any size.
Also try resizing your browser windows to see how your HTML pages look at different sizes.
?
Test every event handler to ensure that it executes as expected.
?
Declare variables explicitly using the var keyword.
?
Use descriptive variable and function names and avoid using single-character names.
?
Pay attention when using object names.
Core JavaScript objects begin with capitalized letters (for example, Array, Boolean, Date, Function, Math, Number, Object, RegExp, and String).
?
Watch your quotation marks.
Remember that quotation marks are used in pairs around strings and that both quotation marks must be of the same style (either single or double). If you want to show quotation marks as part of the text message, embed them inside quotation marks of the alternate type (for example, place single quotation marks inside double quotation marks and vice versa).
?