javascript使用全集2
2011年04月08日
3.3 写出字符串
document.write("");
3.2 document.from
指定 表单域
3.1 document.all.id
查找指定 HTML 页面 ID 或 名字
3. document
2.2 转到特定网页
onClick="location.href=' http://127.0.0.1/'"
2.1 单独 location
取页面地址
2. location
1.4 this.height
取高度属性
1.3 this.width
取宽度属性
1.2 this.href
取链接地址
1.1 this.src
取图片地址
1. this
---/---------------------------------------------- -----------
问题集:
3. return 返回多个值 2006-10-14 14:13:18
2. 理解 setInternal 用法
1. 理解 void 用法
---/---------------------------------------------- -----------
相关摘要:
24. Keep in mind that the function statement is available in all versions of JavaScript, the Function( ) constructor is available only in JavaScript 1.1 and later, and function literals are available only in JavaScript 1.2 and later. Recall that we said the three functions defined earlier are "more or less" equivalent -- there are some differences between these three techniques for function definition, which we'll consider in Section 11.5.
//JavaScript: The Definitive Guide, 4th Edition -- 7.1.3 Function Literals
2006-10-19 14:35:56
23. The Function( ) constructor expects any number of string arguments. The last argument is the body of the function -- it can contain arbitrary JavaScript statements, separated from each other by semicolons. All other arguments to the constructor are strings that specify the names of the parameters to the function being defined. If you are defining a function that takes no arguments, you simply pass a single string -- the function body -- to the constructor.
//JavaScript: The Definitive Guide, 4th Edition -- 7.1.2 The Function( ) Constructor
2006-10-19 14:16:18
22. Note that ECMAScript v3 does not allow function definitions to appear anywhere; they are still restricted to top-level global code and top-level function code. This means that function definitions may not appear within loops or conditionals, for example.[1] These restrictions on function definitions apply only to function declarations with the function statement. As we'll discuss later in this chapter, function literals (another feature introduced in JavaScript 1.2 and standardized by ECMAScript v3) may appear within any JavaScript e??xpression, which means that they can appear within if and other statements.
//JavaScript: The Definitive Guide, 4th Edition -- 7.1 Defining and Invoking Functions
2006-10-19 14:10:13
21. try and finally can be used together without a catch clause. In this case, the finally block is simply cleanup code that is guaranteed to be executed, regardless of any break, continue, or return statements within the try clause. For example, the following code uses a try/finally statement to ensure that a loop counter variable is incremented at the end of each iteration, even when an iteration terminates abruptly because of a continue statement:
//JavaScript: The Definitive Guide, 4th Edition -- 6.17 try/catch/finally
2006-10-18 19:18:08
20. The continue statement, in both its labeled and unlabeled forms, can be used only within the body of a while, do/while, for, or for/in loop. Using it anywhere else causes a syntax error.
//JavaScript: The Definitive Guide, 4th Edition -- 6.12 continue
2006-10-18 16:03:46
19. The following rules are used to determine whether two values are identical according to the === operator:
If the two values have different types, they are not identical.
If both values are numbers and have the same value, they are identical, unless either or both values are NaN, in which case they are not identical. The NaN value is never identical to any other value, including itself! To check whether a value is NaN, use the global isNaN( ) function.
If both values are strings and contain exactly the same characters in the same positions, they are identical. If the strings differ in length or content, they are not identical. Note that in some cases, the Unicode standard allows more than one way to encode the same string. For efficiency, however, JavaScript string comparison compares strictly on a character-by-character basis, and it assumes that all strings have been converted to a "normalized form" before they are compared. See the "String.localeCompare( )" reference page in the core reference section of this book for another way to compare strings.
If both values are the boolean value true or both are the boolean value false, they are identical.
If both values refer to the same object, array, or function, they are identical. If they refer to different objects (or arrays or functions) they are not identical, even if both objects have identical properties or both arrays have identical elements.
If both values are null or both values are undefined, they are identical.
The following rules are used to determine whether two values are equal according to the == operator:
If the two values have the same type, test them for identity. If the values are identical, they are equal; if they are not identical, they are not equal.
If the two values do not have the same type, they may still be equal. Use the following rules and type conversions to check for equality:
If one value is null and the other is undefined, they are equal.
If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.
If either value is true, convert it to 1 and try the comparison again. If either value is false, convert it to 0 and try the comparison again.
If one value is an object and the other is a number or string, convert the object to a primitive and try the comparison again. An object is converted to a primitive value by either its toString( ) method or its valueOf( ) method. The built-in classes of core JavaScript attempt valueOf( ) conversion before toString( ) conversion, except for the Date class, which performs toString( ) conversion. Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way.
Any other combinations of values are not equal.
As an example of testing for equality, consider the comparison:
"1" == true
This e??xpression evaluates to true, indicating that these very different-looking values are in fact equal. The boolean value true is first converted to the number 1, and the comparison is done again. Next, the string "1" is converted to the number 1. Since both numbers are now the same, the comparison returns true.
When the equality operator in JavaScript 1.1 attempted to convert a string to a number and failed, it displayed an error message noting that the string could not be converted, instead of converting the string to NaN and returning false as the result of the comparison. This bug has been fixed in JavaScript 1.2.
//JavaScript: The Definitive Guide, 4th Edition -- 5.4 Equality Operators
2006-10-16 21:56:30
18. In top-level code (i.e., JavaScript code that is not part of a function), you can use the JavaScript keyword this to refer to the global object. Within functions, this has a different use, which is described in Chapter 7.
//JavaScript: The Definitive Guide, 4th Edition -- 4.6 Variables as Properties
2006-10-16 16:08:34
17. Garbage collection is automatic and is invisible to the programmer. You can create all the garbage objects you want, and the system will clean up after you! You need to know only enough about garbage collection to trust that it works; you don't have to wonder about where all the old objects go. For those who aren't satisfied, however, Section 11.3, contains further details on the JavaScript garbage-collection process.
//JavaScript: The Definitive Guide, 4th Edition -- 4.5 Garbage Collection
2006-10-16 16:01:55
16. Primitive Types and Reference Types
var a = [1,2,3]; // Initialize a variable to refer to an array
var b = a; // Copy that reference into a new variable
a[0] = 99; // Modify the array using the original reference
alert(b); // Display the changed array [99,2,3] using the new reference
If this result does not seem surprising to you, you're already well familiar with the distinction between primitive and reference types. If it does seem surprising, take a closer look at the second line. Note that it is the reference to the array value, not the array itself, that is being assigned in this statement. After that second line of code, we still have only one array object; we just happen to have two references to it.
//JavaScript: The Definitive Guide, 4th Edition -- 4.4 Primitive Types and Reference Types
15. 在任何情况下,如果六个月以后您还能毫不费力地阅读和理解所编写的代码,则说明这些代码写得不错。
//Windows 脚本技术 -- JScript -- 什么是 JScript?
2006-10-16 14:12:55
14. The rule that all variables declared in a function are defined throughout the function can cause surprising results. The following code illustrates this:
var scope = "global";
function f( ) {
alert(scope); // Displays "undefined", not "global"
var scope = "local"; // Variable initialized here, but defined everywhere
alert(scope); // Displays "local"
}
f( );
//JavaScript: The Definitive Guide, 4th Edition -- 4.3 Variable Scope
13. Variable Scope
The scope of a variable is the region of your program in which it is defined. A global variable has global scope -- it is defined everywhere in your JavaScript code. On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope. Function parameters also count as local variables and are defined only within the body of the function.
Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable. For example, the following code prints the word "local":
var scope = "global"; // Declare a global variable
function checkscope( ) {
var scope = "local"; // Declare a local variable with the same name
document.write(scope); // Use the local variable, not the global one
}
checkscope( ); // Prints "local"
//JavaScript: The Definitive Guide, 4th Edition -- 4.3 Variable Scope
2006-10-16 1:11:59
12. In general, functions do not know what variables are defined in the global scope or what they are being used for. Thus, if a function uses a global variable instead of a local one, it runs the risk of changing a value upon which some other part of the program relies. Fortunately, avoiding this problem is simple: declare all variables with var
//JavaScript: The Definitive Guide, 4th Edition -- 4.3 Variable Scope
2006-10-15 22:23:32
11. If you attempt to read the value of an undeclared variable, JavaScript will generate an error. If you assign a value to a variable that you have not declared with var, JavaScript will implicitly declare that variable for you. Note, however, that implicitly declared variables are always created as global variables, even if they are used within the body of a function. To prevent the creation of a global variable (or the use of an existing global variable) when you meant to create a local variable for use within a single function, you must always use the var statement within function bodies. It's best to use var for all variables, whether global or local. (The distinction between local and global variables is explored in more detail in the next section.)
//JavaScript: The Definitive Guide, 4th Edition -- 4.2.1 Repeated and Omitted Declarations
2006-10-15 22:00:53
10. Error Objects
ECMAScript v3 defines a number of classes that represent errors. The JavaScript interpreter "throws" an object of one of these types when a runtime error occurs. (See the throw and try statements in Chapter 6 for a discussion of throwing and catching errors.) Each error object has a message property that contains an implementation-specific error message. The types of predefined error objects are Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, and URIError. You can find out more about these classes in the core reference section of this book.
//JavaScript: The Definitive Guide, 4th Edition -- 3.11 Error Objects
2006-10-15 19:39:08
9. undefined
Another special value used occasionally by JavaScript is the undefined value returned when you use either a variable that has been declared but never had a value assigned to it, or an object property that does not exist. Note that this special undefined value is not the same as null.
Although null and the undefined value are distinct, the == equality operator considers them to be equal to one another. Consider the following:
my.prop == null
//JavaScript: The Definitive Guide, 4th Edition -- 3.8 undefined
2006-10-15 18:02:39
8. The way to really learn a new programming language is to write programs with it.
//JavaScript: The Definitive Guide, 4th Edition -- 1.10 Exploring JavaScript
2006-10-14 2:40:02
7. Both Netscape and Microsoft have made their JavaScript interpreters available to companies and programmers who want to embed them in their applications. Netscape's interpreter was released as open source and is now available through the Mozilla organization (see http://www.mozilla.org/js/). Mozilla actually provides two different versions of the JavaScript 1.5 interpreter. One is written in C and is called "SpiderMonkey." The other is written in Java and, in a flattering reference to this book, is called "Rhino."
//JavaScript: The Definitive Guide, 4th Edition -- 1.4 JavaScript in Other Contexts
2006-10-14 1:13:44
6. 11.1.2 Explicit Type Conversions
Table 11-1 listed the automatic data type conversions that JavaScript performs. It is also possible to explicitly convert values from one type to another. JavaScript does not define a cast operator as C, C++, and Java do, but it does provide similar facilities for converting data values.
As of JavaScript 1.1 (and the ECMA-262 standard), Number( ) , Boolean( ), String( ), and Object( ) may be called as functions as well as being invoked as constructors. When invoked in this way, these functions attempt to convert their arguments to the appropriate type. For example, you could convert any value x to a string with String(x) and convert any value y to an object with Object(y).
There are a few other tricks that can be useful for performing explicit conversions. To convert a value to a string, concatenate it with the empty string:
var x_as_string = x + "";
To force a value to a number, subtract zero from it:
var x_as_number = x - 0;
And to force a value to boolean, use the ! operator twice:
var x_as_boolean = !!x;
Because of JavaScript's tendency to automatically convert data to whatever type is required, explicit conversions are usually unnecessary. They are occasionally helpful, however, and can also be used to make your code clearer and more precise.
//JavaScript: The Definitive Guide, 4th Edition -- 11.1 Data Type Conversion
2006-10-12 12:36:23
5. Each RegExp object has five properties. The source property is a read-only string that contains the text of the regular e??xpression. The global property is a read-only boolean value that specifies whether the regular e??xpression has the g flag. The ignoreCase property is a read-only boolean value that specifies whether the regular e??xpression has the i flag. The multiline property is a read-only boolean value that specifies whether the regular e??xpression has the m flag. The final property is lastIndex, a read-write integer. For patterns with the g flag, this property stores the position in the string at which the next search is to begin. It is used by the exec( ) and test( ) methods, as described in the previous section.
//JavaScript: The Definitive Guide, 4th Edition -- 10.3.2 RegExp Instance Properties
2006-10-9 21:23:26
4. the RegExp constructor. search( ) does not support global searches -- it ignores the g flag of its regular e??xpression argument.
//JavaScript: The Definitive Guide, 4th Edition -- 10.2 String Methods for Pattern Matching
2006-10-9 19:56:40
3. We've seen the . operator used to access the properties of an object. It is also possible to use the [] operator, which is more commonly used with arrays, to access these properties. Thus, the following two JavaScript e??xpressions have the same value:
object.property
object["property"]
//JavaScript: The Definitive Guide, 4th Edition -- 8.6 Objects as Associative Arrays
2006-10-9 14:45:13
2. The typeof Operator
typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.
The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value. It evaluates to "object" for objects, arrays, and (surprisingly) null. It evaluates to "function" for function operands and to "undefined" if the operand is undefined.
//JavaScript: The Definitive Guide, 4th Edition -- 5.10.2 The typeof Operator
1. JavaScript strings (and JavaScript arrays, as we'll see later) are indexed starting with zero.
---/---------------------------------------------- -----------
小小心得
1. 花括弧定义 对象 元素, 方括弧定义 数组 元素. 2006-10-15 17:55:50
2. Javascript 正则表达式摘要
linenum
Javascript 与正则表达式 By shawl.qiu
语法方法:
5. str.split()
//');
document.write(str.split(/"s*,"s*/).join());
//]]>
4. str.match()
//');
document.write(str.match(/"w+/g).join());
//]]>
3. str.replace()
//');
document.write(str.replace(/^("s+)|("s+)$/g,'----- -----'));
//]]>
2. str.search()
//
1. 子匹配
//
0. 创建正则表达式模式
0.1 new RegExp()
//
-1. re.exec(str)
//');
document.write(('match index: ').bold()+(report.index+'').fontcolor('red')+'' );
document.write(('match lastIndex: ').bold()+(pt.lastIndex+'').fontcolor('red')+'') ;
}
//]]>
-2. re.test(str)
//