javascript研究

来源:互联网 发布:如何更新mac os版本 编辑:程序博客网 时间:2024/05/16 17:51

如果在一个statement中break lines,那么就要让被分的上句不是一个完整的语句
像这样:上句var str = “hello world” +  下句 something + “这样才行”;
因为,javascript总是在完整的语句后如果你没有“;”号就自己在换行处加上,这样就有可能让你的语义不正确。

这样定义函数var method1 = var function(paramerters list) {…..}  这样有时叫做lambda function

在对象构建时,对象的属性不一定为标识符,可以为字符串;属性的値也不一定要为字符串,可以是任意的javascript表达式
像这样:var square = { “upperLeft”: { x:point.x, y:point.y },
‘lowerRight’: { x:(point.x + side), y:(point.y+side) }};

Associative arrays are indexed by strings
Regular arrays are indexed by nonnegative integers
javascript不支持多维数组,但你可以这样 var matrix = [[1,2,3], [4,5,6], [7,8,9]];
还可以这样定义数组 var sparseArray = [1,,,,5];

null和undefined不是一回事,但 null == undefined is true!
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.
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”.

null is usually considered a special value of object typea value that represents no object. null is a unique value.
When a variable holds the value null, you know that it does not contain a valid object, array, number, string, or boolean value.
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”.

注意javascript中,时间数据类型Date中,月从零开始计,也就是说0月是January。

个人觉得完全没有必要去 var str = new String(”Hello World”);尽管如果 var str = “Hello”;在 str.substring()时会生成一个临时的String对象
但因为在你做+的时候它自动生成一个临时的基本类型的string_str_transient = “Hello World”这样也会有一定的开消,虽然会自动被discard

如果你没有用var去声明一个变量,那么javascript会自动为你声明一个全局的变量,也许这不是你想要的,所以最好自己坚持在声明变量时用var

注意,javascript没有block-level的变量范围

1 + 2 // Addition. Result is 3.
“1″ + “2″ // Concatenation. Result is “12″.
“1″ + 2 // Concatenation; 2 is converted to “2″. Result is “12″.
11 < 3 // Numeric comparison. Result is false.
“11″ < “3″ // String comparison. Result is true.
“11″ < 3 // Numeric comparison; “11″ converted to 11. Result is false.
“one” < 3 // Numeric comparison; “one” converted to NaN. Result is false.

 

尽量避免使用with语句,因为它会使你的程序变慢和可读性变差

When you intentionally use the empty statement, it is a good idea to comment your code in a way that makes it clear that you are doing it on purpose.
For example: for(i=0; i < a.length; a[i++] = 0) /* Empty */ ;

typeof: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.

constructor:它是任何对象的一个属性,用来init

instanceof:instanceof operator checks the value of the constructor property

如果想让一个函数去使用一个值来保存它的一些信息,完整可以不定义全局变量,用它自己的属性就OK
// Create and initialize the “static” variable.
// Function declarations are processed before code is executed, so
// we really can do this assignment before the function declaration.
uniqueInteger.counter = 0;

// Here’s the function. It returns a different value each time
// it is called and uses a “static” property of itself to keep track
// of the last value it returned.
function uniqueInteger() {
// Increment and return our “static” variable
return uniqueInteger.counter++;
}

Object is the superclass of all the built-in classes,
and all classes inherit a few basic methods from Object.

如果试图去读取一个未声明的全局变量时,会throw a exception,
但如果是读取一个未声明的对象属性就只会得到undefined而已。

原创粉丝点击