JavaScript学习笔记

1、JavaScript does not support multidimensional arrays,except as arrays of arrays.

2、JavaScript is an untyped language, the elements of an array do not all need to be of the same type, as they do in typed languages like Java.

3、Arrays can be created with the Array( ) constructor function.
var a = new Array( );
a[0] = 1.2;
a[1] = "JavaScript";
a[2] = true;
a[3] = { x:1, y:3 };

4、Arrays can also be initialized by passing array elements to the Array( ) constructor. var a = new Array(1.2, "JavaScript", true, { x:1, y:3 });

5、If you pass only a single number to the Array( ) constructor, it specifies the length of the array. Thus:
var a = new Array(10);
creates a new array with 10 undefined elements.

6、Also, as with object literals, the elements in array literals can be arbitrary expressions and need not be restricted to constants:
var base = 1024;
var table = [base, base+1, base+2, base+3];

7、The JavaScript keyword null is a special value that indicates no value. null is usually considered a special value of object typea value that represents no object.

8、When null is used in a Boolean context, it converts to false. When used in a numeric context, it converts to 0. And when used in a string context, it converts to "null".

9、Another special value used occasionally by JavaScript is the undefined value. undefined is 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.

10、Although null and the undefined value are distinct, the == equality operator considers them equal to one another. However, if you truly must distinguish between a null value and an undefined value, use the === identity operator or the typeof operator.

11、When the undefined value is used in a Boolean context, it converts to false. When used in a numeric context, it converts to NaN. And when used in a string context, it converts to "undefined".

12、JavaScript can flexibly convert values from one type to another. When you use a string in an object contexti.e., when you try to access a property or method of the stringJavaScript internally creates a String wrapper object for the string value. This String object is used in place of the primitive string value. The object has properties and methods defined, so the use of the primitive value in an object context succeeds.

13、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.

14、If one operand is or converts to a string, and one is or converts to a number, the operator attempts to convert the string to a number and performs a numerical comparison. If the string does not represent a number, it converts to NaN, and the comparison is false. (In JavaScript 1.1, the string-to-number conversion causes an error instead of yielding NaN.),for example:
var str1 = "notANumber";
var int1 = 1234;
var test1 = (str1 < int1); //return false;
var test2 = (str1 > int1); //return false;
var test3 = (str1 == int1); //return false;
So we can not compare a string with a number as following:
if (str1 < int1) {
document.write("str1 < int1");
} else {
document.write("str1 >= int1");
}
The result is obviously wrong.Thus, we should check the string with isNaN before comparison.

15、If the operands of the comparison operators cannot both be successfully converted to numbers or to strings, these operators always return false.

16、If either operand is or converts to NaN, the comparison operator always yields false.

17、When combining JavaScript and HTML, it is a good idea to use one style of quotes for JavaScript and the other style for HTML.
<a href="" οnclick="alert('Thank you')">Click Me</a>

18、In some implementations of JavaScript, individual characters can be read from strings (but not written into strings) using array notation, so the earlier call to charAt( ) can also be written like this:
last_char = s[s.length - 1];
Note, however, that this syntax is not part of the ECMAScript v3 standard, is not portable, and should be avoided.

19、If a number is used where a boolean value is expected, the number is converted to TRue unless the number is 0 or NaN, which are converted to false. If a string is used where a boolean value is expected, it is converted to true except for the empty string, which is converted to false. null and the undefined value convert to false, and any non-null object, array, or function converts to true.

20、The fact that functions are true values in JavaScript gives a lot of flexibility to the language. It means that functions can be stored in variables, arrays, and objects, and it means that functions can be passed as arguments to other functions.

21、Objects in JavaScript can serve as associative arrays
image["width"]
image["height"]

22、The property values used in object literals need not be constants; they can be arbitrary JavaScript expressions. Also, the property names in object literals may be strings rather than identifiers:

var square =
{ "upperLeft": { x:point.x, y:point.y },
'lowerRight': { x:(point.x + side), y:(point.y+side) }};

23、typeof(i)
primitive: "number", "string", "boolean"
arrays, null, String, Number, Boolean, Date,
RegExp, objects, all client-side objects: "object"
function: "function"
undefined: "undefined"

24、typeof is useful only to distinguish objects from other, primitive types. In order to distinguish one object type from another, you must use other techniques, such as the instanceof operator.

25、The most common use for ‘void’ is in a client-side javascript: URL, eg.
<a href="javascript:void window.open( );">Open New Window</a>
When a new window is created, the function will return the window object(the new one) if nothing else. And since this code is called from href attribute, the browser will attempt to go to that returned text, which is the object's toString result:object [object]. So a blank screen usually appears with just that text on it(your original page disappears). Using void operator means that no return is passed to the href.In fact that javascript pseudo-URL technique is obsolete and is best to use:
<a href="#" οnclick="window.open( ); return false;">
Open New Window</a>
Besides which, if you use the void() method, and your viewer happens to right-click on the link, and then chooses "Open link in a new window" - a nasty error results. Using onClick() instead at least avoids the error.

26、For 'switch' statement, the matching case is determined using the === identity operator, not the == equality operator, so the expressions must match without any type conversion.

27、[color=brown]for ([i]variable [/i]in [i]object[/i])
statement[/color]
Before the body of the loop is executed, the [b]name[/b] of one of the object's properties is assigned to [i]variable[/i], as a [b]string[/b]. Note that it is the properties' name, not the values. The variable in the for/in loop may be an arbitrary expression, this expression is evaluated each time through the loop, which means that it may evaluate differently each time. For example, you can use code like the following to copy the names of all object properties into an array:
[color=brown]var o = {x:1, y:2, z:3};
var a = new Array( );
var i = 0;
for(a[i++] in o) /* empty loop body */;
document.write(a[1]) [/color][color=green]//array a has been evaluated, and a[1] is 'y' not 2 [/color]

28、function definitions occur at parse time rather than at runtime(parse time is earlier than runtime ).Consider the following code:
[color=brown]
alert(f(4)); // Displays 16. f( ) can be called before it is defined.
var f = 0; // This statement overwrites the property f.
function f(x) { // This "statement" defines the function f before either
return x*x; // of the lines above are executed.
}
alert(f); // Displays 0. f( ) has been overwritten by the variable f.
[/color]

29、Array and Object
[color=green]/* Array is a special object, so it can have properties */[/color]
[color=brown]var a = [1, 2, 3, 4];
a["new"] = 16;
document.write(a.length); [/color][color=green]//4, a["new"] is not a new element of array, but a property[/color]
[color=brown]document.write(a.new); [/color][color=green]//16

/* Adding array elements to an object does not make it an array */ [/color][color=brown]
var o = {x:1, y:2};
o[0] = 21;
document.write(o.length); [/color][color=green]//undefined[/color]
[color=brown]document.write(o.0); [/color][color=green]//error[/color]
[color=brown]document.write(o[0]); [/color][color=green]//21[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值