JavaScript读书笔记

1、带参数的函数编写
<script language="JavaScript" tpye="text/javascript">
<!--
   var farbe;
   function hintergrund(farbe) {
   document.bgColor=farbe;
   }
//-->
</script>

注意:

(1)参数“farbe”的声明是在函数体外的,或者不声明参数也可以,但是不能在参数列表里指定参数类型,即使用var类型也不行,象下面这样是不行的:

function hintergrund(var farbe) {
   document.bgColor=farbe;
   }

(2)函数有返回值时,直接用“return XXX”就行了,不用声明返回值类型。

这两点和非脚本语言是不一样的。

估计在函数体外声明也相当于没有声明

2、伪连接

<a href="javascript: alert('hello I am a pseudo-URL script');">Click
to invoke</a>

<a href="javascript: x=5;y=7;alert('The sum = '+(x+y));">Click to invoke</a>

<a href="/errors/noscript.html"onclick=" alert('hello I am a pseudo-URL 
script');return false;">Click to invoke</a>

3、大于、小于、大于等于、小于等于运算符比较特殊

<< (less than), >> (greater than), <<= (less than or equal to), and >>= (greater than or equal to).

4、for…in而不是foreach

One interesting variation of the for loop is the for/in construct. This construct allows us to loop through the various properties of an object. For example, we could loop through and print the properties of a browser’s window object using a for/in statement like this:

var aProperty
for (aProperty in window)
 {
   document.write(aProperty)
   document.write("<<br />>");
}

5、正则表达式

var country = new RegExp("England");
var geographicLocation = "New England";
document.write("Destination for work: "+geographicLocation+"<<br />>");
geographicLocation = geographicLocation.replace(country, "Zealand");
document.write("Destination for vacation: "+geographicLocation);

6、强类型定义方法

JavaScript本身是弱类型语言,如不作特殊声明,变量的类型是可以根据内容而改变的,但是变量可以声明为强类型变量,如:

var favNumber : number;

Given this example, an assignment like

favNumber = 3;

would be perfectly valid. But if you assigned some non-numeric type to the variable like

favNumber = "San Diego";

7、基本数据类型

number, string, Boolean, undefined, and null

Boolean :true和false

null和 undefined的区别

The null value indicates an empty value; it is essentially a placeholder that represents “nothing.” The distinction between undefined and null values is tricky. In short, undefined means the value hasn’t been set, whereas null means the value has been set to be empty

var x;  //x是undefined类型

var y = null; //y是undefined类型,且是null

8、isNaN函数

The following example illustrates the use of both techniques:

var x = 0 / 0;            // assign NaN to x
if (x != x)               // check via self-equality
{
// do something
}
if (isNaN(x))             // check via explicit call
{
// do something
}

好像是超出最大值或最小值了,就变成NaN(不确定的值)了

9、全都是string,没有char类型,但是可以进行提取字符的操作

var myName = "Thomas";
var thirdLetter = myName.charAt(2);

this code fragment extracts the third character from the string (o) and assigns it to the variable thirdLetter――提取第三个字符,形成字符串string(o),赋给thirdLetter变量

var strlen = myName.length();

上面的代码设置strlen == 6,这也和C语言的串串不一样!

10、JavaScript中的转义字符和C语言里的一样,也是“\ + 特殊字符”

如:换行用“\n”表示

11、语言提供的组合类型

Objects、arrays、functions

Examples of such objects are Date, Math, and RegExp. Finally, each data type in JavaScript has a corresponding object. So there are String, Number, Boolean, Array, and even

(1)创建组合对象

var myString = new String();

(2)JavaScript中可以动态增加对象属性!!!!!!!!

var myLocation = new Object();
myLocation.city = "San Francisco";
myLocation.state = "California";

(3)数组

myArray[5] = "Hamburgers are nice, sushi is better.";
var x = myArray[5];

var myArray = [2, 4, 6, 8, "ten"];

var myArray = [];

var myArray = new Array();

12、基本类型和基本类型的封装类型的关系表

Type

Result

Undefined

undefined

Null

object

Boolean

boolean

Number

number

String

string

Object

object

Function

function

13、命名原则

JavaScript 由于脚本下载需要时间,建议在符合命名规范的情况下,使得名字尽量的短。

14、用函数创建变量(Functions as Objects)

语法:

varfunctionName = newFunction("argument 1",..."argument n", 
"statements for function body");

(1)不带参数的

var sayHello = new Function("alert('Hello there');");

Later on we can then use the assigned variable sayHello just like a regular function call:

sayHello();

(2)带参数的

var sayHello2 = new Function("msg","alert('Hello there '+msg);");

and call it:

sayHello2('Thomas');

(3)复杂应用

function SimpleRobot(robotName)
{
   this.name = robotName;
   this.sayHi = function () { alert('Hi my name is '+this.name); };
   this.sayBye = function () { alert('Bye!'); };
   this.sayAnything = function (msg) { alert(this.name+' says '+msg); };
}

It is now simple to create an object using the new operator in conjunction with our SimpleRobot constructor function, as shown here:

var fred = new SimpleRobot("Fred");

Invoking the various functions, or, more correctly, methods, is simply a matter of invoking their names, similar to plain function calls:

fred.sayHi();
fred.sayAnything("I don't know what to say");
fred.sayBye();

15、JavaScripts中的Objects

Type

Example

Implementation Provided By

Governing Standard

User-defined

Programmer-defined Customer or Circle

Programmer

None

Built-in

Array, Math

The browser via its JavaScript engine

ECMA-262

Browser

Window, Navigator

The browser

None (though some portions adhere to an ad hoc standard)

Document

Image, HTMLInputElement

The browser via its DOM engine

W3C DOM

16、打印文档(document)中所有元素属性

for (var prop in document)

document.write('document["' + prop + '"] = ' + document[prop] + '<br />');

17、打印窗口(window)中所有元素属性

var aProperty

for (aProperty in window)

{

document.write(aProperty)

document.write("<<br />>");

}

18、打印Navigator中所有元素属性

var aProperty;
document.write("<h1>Navigator Object Properties</h1>");
for (aProperty in navigator)
{
  document.write(aProperty);
  document.write("<br />");
}

19、打印自定义对象中所有元素属性

var myString = new String("Niels is a poor foosball player");
myString.aboutFoosball = true;
for (var prop in myString)
  document.write('myString["' + prop + '"] = ' + myString[prop] + '<br />');

20、JavaScript中可以使用with

with (document.myForm) 
{
  if (username.value == "")
    alert("Must fill in username");
  if (password.value == "")
    alert("Password cannot be blank. ");
}

21、JavaScript中基本类型变量传值,对象类型传引用

未完...

======================

JavaScript.2.0教程:McGraw.Hill.Osborne.JavaScript.2.0.The.Complete.Reference.Second.Edition.eBook-LiB.chm