6 语句
这个Javascript和C++,Java几乎是一抹一样的,只是function的定义和C++/Java有些不同。首先,function是可以嵌套的,不过function的定义只能嵌套在function里面。还有,function是编译期就决定的,所以如果有变量名和它同名,就会有些“奇怪”的现象,看下面的代码:
var point = new Object();point.x = 10;point.y = 20;for (var pro in point) {document.write("name: " + pro + "; value: " + point[pro] + "<br>");} var arr = [1,2,3,4,5];for (var item in arr) {document.write(item + "<br>");} document.write(f(4) + "<br>");var f = 10;function f(x) {return x * x;} document.write(f);
?最后的结果是:
name: x; value: 10
name: y; value: 20
0
1
2
3
4
16
10