js循环、异常、函数
1.for循环
<p>a</p>
<p>b</p>
<p>c</p>
<div>www</div>
<script>
var eles_p=document.getElementsByTagName("p")
console.log(eles_p);
for (var i=0;i<eles_p.length;i++){
console.log(i);
console.log(eles_p[i]);
}
</script>
2.while循环
//while循环
var x=0;
var i=1;
while (i<=100){
x+=i;
i++;
}
3.异常和异常抛出
//异常
try {
console.log(123);
throw Error("define error")//抛出异常
}
catch (e) {
console.log(e);
}
finally {
console.log('ff');
}
4.//创建字符串对象
var str1="hello";
var str2=new String("hello1");
console.log(typeof str1);
console.log(typeof str2);
4.数组
//数组方法 join()
var s1=[1,2,3,4];
console.log(s1.join("--"));
// Tostring()
var q1=s1.toString();
console.log(q1);
//concat()
var ss1=[1,2,2];
console.log(ss1.concat());
//reverse()反转
var as=[23,45,55];
console.log(as.reverse());
//sort()排序
console.log(sa.sort());
//splice()子数组删除和插入
//push,pop数组进出栈,在后操作
var ww=[1,2,3];
ww.push([4,5,6]);
console.log(ww);
console.log(ww.pop())
//shift unshift,与上两个一样,在前操作
var qq=[2,3,4];
qq.unshift([111,222]);
console.log(qq);
console.log(qq.shift());
5.函数
//函数的创建方式一
function f(x,y) {
alert(123);
return x+y;
}
console.log(f(23,5678));
//函数的调用
function f(x,v,z) {
return x+v+z
}
console.log(f(1,2,3));
6.匿名函数
console.log(f2(1,2,34,5,6));
//匿名函数方式一
var func=function (arg) {
alert(arg)
};
func('kkk');
//匿名方式二
(function (arg) {
alert(arg)
})("YUAN")