Nomad & His Life

博观而约取,厚积而薄发
posts - 15, comments - 88, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

《Learn JavaScript in a Weekend》导读+笔记

Posted on 2006-08-03 12:56 Nomad 阅读(690) 评论(1)  编辑  收藏 所属分类: 原版阅读

o_159200086X_140_34O.gif

这是一本JavaScript的入门书籍。作者用通俗易懂的语言讲述JavaScript的基本内容,由书名就能看得出来,读者只需要一个周末就能将它看完。书的最后两章顺带介绍了微软的JScript和WSH。可以在http://www.cnshare.org 上下载。

星期五晚上:"Introducing JavaScript and JScript."
介绍了JavaScript和JScript的历史知识和基本情况。

星期六早晨:"Learning the Basics of JavaScript Coding."
讲解JavaScript的基本语法、函数、数组,以及基于对象的特性。

星期六中午:"Using JavaScript to Build Better Web Pages."
详细描述了JavaScript对象的特性。首先介绍了Browser里的document、form、location等特殊类,然后介绍如何编写自己的对象,最后介绍如何处理Frames和表单。

星期六晚上:"Doing Really Cool Things with Your Web Pages."
介绍了一些特别的东西。比如如何控制状态栏,如何控制浏览器,展示各种不同的弹出窗口,让JavaScript工作在不同的浏览器中,用JavaScript制作的动画。

星期天早晨:"Advanced JavaScript Coding."
JavaScript高级编程。首先介绍的是cookie操作,然后是写JavaScript的一些经验,最后用了很大的篇幅介绍一个在线书店的制作。

星期天中午: "Learning How to Use JScript and the WSH."
略。

星期天晚上: "Using JScript to Automate Windows Tasks."
略。



方法

for in循环

用来遍历一个特殊对象的全部属性。

for (variable in object) {

  statements;

}

每次循环这个object的一个属性就被设为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 方法

这是一个简便的方法来执行对某个object的操作。

with (object) {

  statements;

}

在括号里的语句可以执行object的方法,而不用多敲"object."。例如,将object换为document,在statements中只要写write(...),就相当于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>

当然,这个方法除了省去你一些敲键盘的时间外,没有其他的功能。

 

 

 

 

 

 

数组

 

排序数组

数组里有sort()方法来排序它的内容,直接看示例:

<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>

 

 

 

 

对象

 

Function 对象

JavaScript 同意你创建Function对象。对于一般的方法并不适用。这样可以让你更好的组织你的方法,但是由于Function对象固有的效率问题,最好少用。

语法:FunctionName = new Function([p1,.......pn] body)

示例:Warn = new Function("alert('HI!')");

 

 

Math 对象

下面列出了Math对象的一些属性和方法。

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 对象

Object 对象是所有对象的基础。

MyNumberObject = new Object(99.99);

MyStringObject = new Object("Testing");

 

 

 

String 对象

下面列出了String对象的常用方法。

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

 

 

 

 

 

浏览器对象

window 对象

关于窗口的操作

window.alert()

window.close()

window.forward()

window.back()

window.home()

window.resizeTo(300,400)

 

 

document 对象

最常用的对象!

_         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

 

 

 

 

 

高效程序员的习惯

 

写大量的注释。

 

注意缩距让你的代码易读。

 

尽量将代码模块化。

 

在脚本的顶端声明方法和变量。

 

以一致的方式命名你的变量和方法。

 

用一致的语法命名变量和方法。

 

不要仅运行了一次而认为你的代码没有bug

 

用不支持JavaScript的浏览器运行你的代码,让你的脚本有一个正确的输出。

 

测试所有可能的场景。

 

写一点测试一点。

 

以浏览器不同的大小来测试你页面的输出,保证它们都是你想要的。

 

测试每个事件处理器。

 

用变量的关键字明确的声明变量。

 

方法或变量名用描述性的语言,而不用单个字母。

 

取对象名时小心系统已经定义的名字。

 

注意引号里的字符。

 

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).

 


评论

# re: 《Learn JavaScript in a Weekend》导读+笔记  回复  更多评论   

2006-08-10 19:27 by 坎井之蛙
等发工资,去买。。。,呵呵!

只有注册用户登录后才能发表评论。


网站导航: