1.switch可用于字符串: java
3 |
case 1: alert("number");break; |
4 |
case true:alert('boolean');break; |
5 |
case 'abc': alert('String');break; |
2.with语句:
4 |
}//这样就不用每次都写document.writeln(...) |
3.foreach语句: 数组
1 |
function Person(name ,gender){ |
4 |
this.shout = function (){return 'shut up !';} |
6 |
var p = new Person('macondo','male'); |
8 |
document.writeln(i+' = '+p[i]+'<br>'); |
这里foreach语句比java中得更进一步,缘由是p.name也能够写成p['name']
4.数组:
01 |
var fruit = new Array(); |
02 |
fruit.push('apple');//等同于fruit[0] = 'apple' |
03 |
fruit.push('pear');//等同于fruit[1] = 'pear' |
04 |
//等同于var fruit = new Array('apple','pear'); |
05 |
//等同于var fruit = ['apple','pear']; |
06 |
fruit['a']='orange';//我以为就像fruit.a='orange',其实它再也不数组里,而是fruit的一个属性 |
09 |
document.writeln(i+' = '+fruit[i]+'<br>'); |
11 |
var t = fruit.length;//这里必须先把length赋给t |
12 |
for(var i=0;i<t;i++){//若再for循环里用length, pop()一下length就被改变了 |
13 |
document.writeln('pop:'+fruit.pop()+'<br>'); |
15 |
document.writeln(fruit['a']);//下目标为‘a'的元素没被删除,且不会被pop掉 |
5.js中的数据类型有:string,boolean,number,undefined,function(函数也是对象),其余的都是object类型,能够同typeof运算符获得数据类型 app
01 |
function add(a,b){return a+b;} |
03 |
writeln(typeof e);//undefined |
04 |
writeln(typeof 4.5);//number |
05 |
writeln(typeof (1/0));//number |
06 |
writeln(typeof true);//boolean |
07 |
writeln(typeof '');//string |
08 |
writeln(typeof add);//function |
09 |
writeln(typeof new add());//object |
10 |
writeln(typeof null)//也是object? |
12 |
writeln(typeof new Boolean('abc'));//objcet,由于boolean也是继承自object |
13 |
writeln(typeof Boolean('abc'));//boolean,这里是强制类型转换,把'abc'变成true |
14 |
writeln(typeof new String('abc'));//同上 |
15 |
writeln(typeof String(true)); |
不知为何null居然也是object,个人理解是除了原始数据类型(number,boolean,string等)以外,其余的包括(Boolean,String)都是object继承来的。Boolean和boolean是有一点区别的 函数
6.函数能够先使用,后声明 ui
7.除了在函数里面声明的变量是local variable以外,在for,if等{}块中声明的变量是全局变量 this
8.js numbers are 64-bit floating point numbers (ex. var a=444;) spa
9.字符串可用<、>比较大小 对象