对Array初始化是使用“,”将元素分隔开。
[] // An empty array: no expressions inside brackets means no elements
[1+2,3+4] // A 2-element array. First element is 3, second is 7
Array中的元素自身也可以是Array
var matrix = [[1,2,3], [4,5,6], [7,8,9]];
Object的初始化表达式与Array很相似。
var p = { x:2.3, y:-1.2 }; // An object with 2 properties
var q = {}; // An empty object with no properties
q.x = 2.3; q.y = -1.2; // Now q has the same properties as p
var rectangle = { upperLeft: { x: 2, y: 2 },
lowerRight: { x: 4, y: 5 } };
Object属性的标识符可以是字符串。而且 " 和 ' 都可以使用。
var side = 1;
var square = { "upperLeft": { x: p.x, y: p.y },
'lowerRight': { x: p.x + side, y: p.y + side}};
// This function returns the square of the value passed to it.
var square = function(x) { return x * x; }
JavaScript提供了两种访问Property的语法
expression . identifier
expression [ expression ]
如:
var o = {x:1,y:{z:3}}; // An example object
var a = [o,4,[5,6]]; // An example array that contains the object
o.x // => 1: property x of expression o
o.y.z // => 3: property z of expression o.y
o["x"] // => 1: property x of object o
a[1] // => 4: element at index 1 of expression a
a[2]["1"] // => 6: element at index 1 of expression a[2]
a[0].x // => 1: property x of expression a[0]
如:
f(0) // f is the function expression; 0 is the argument expression.
Math.max(x,y,z) // Math.max is the function; x, y and z are the arguments.
a.sort() // a.sort is the function; there are no arguments.
如:
new Object()
new Point(2,3)
如果该对象的构造函数没有参数,我们也可以省略括号直接写成:
new Object
new Date
JavaScript提供了instanceof操作符,能够判断instanceof左右两边是否为同一对象。
var d = new Date(); // Create a new object with the Date() constructor
d instanceof Date; // Evaluates to true; d was created with Date()
d instanceof Object; // Evaluates to true; all objects are instances of Object
d instanceof Number; // Evaluates to false; d is not a Number object
var a = [1, 2, 3]; // Create an array with array literal syntax
a instanceof Array; // Evaluates to true; a is an array
a instanceof Object; // Evaluates to true; all arrays are objects
a instanceof RegExp; // Evaluates to false; arrays are not regular expressions
JavaScript能够对JavaScript代码作解析,并生成相应的值。这个过程就是通过eval()方法完成的。
eval("3+2") // => 5
eval()需要一个参数。
如果定义一个变量x, 当调用eval(“x”),就会将变量x的值取出作为返回值返回。eval(“x=1”)会把x的值修改成1.
我们可以通过eval()定义变量。eval(“var y=3;“);
还可以通过eval()定义方法。
eval("function f() { return x+1; }");
你可以通过下面这个表达式使用typeof 操作符
(typeof value == "string") ? "'" + value + "'" : value
我们也可以通过下面这种语法来使用typeof操作符
typeof(i)
typeof 的值如下:
x typeof x
undefined "undefined"
null "object"
true or false "boolean"
any number or NaN "number"
any string "string"
any function "function"
any nonfunction native object "object"
any host object An implementation-defined string, but not “undefined”,“boolean”, “number”, or “string”.
instanceof 操作符可以区分不同对象,如 a instanceof Date. 而typeof无法区分具体的对象。
delete操作符可以从Objects删除Object的Property,也可以从arrays删除array item。
如:
var o = { x: 1, y: 2}; // Start with an object
delete o.x; // Delete one of its properties
"x" in o // => false: the property does not exist anymore
var a = [1,2,3]; // Start with an array
delete a[2]; // Delete the last element of the array
a.length // => 2: array only has two elements now
被删除的propery或者array item会从Object或者array中被用久删除,而不是仅仅将它们设置成undefined. 另外我们也可以通过使用in操作符来判断被删除的元素是否还在Object或者array中。
这个操作符会被偶尔使用: 它会解析跟在它之后的表达式,但是不会把表达式的值返回。
void操作符的格式如下:
javascript:void(expression);
javascript:void expression;
如:
<a href="javascript:void window.open();">Open New Window</a>
这样子写的话,就不需要定义onclick事件。
6/3/2014 5:02:45 PM