JS你可能还不知道的一些知识点(一)
js程序是用Unicode字符集编写的,
2、转义字符:反斜线
|
1
2
3
4
|
function Test(){ var s='you\'re right,it can\'t be a quote'; console.log(s);} |
3、slice方法:方法可从已有的数组中返回选定的元素 arrayObject.slice(start,end)
start 必选 如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。
end 可选 规定从何处结束选取
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function Test() { var arr = new Array(3); arr[0] = "George"; arr[1] = "John"; arr[2] = "Thomas"; arr[3] = "James"; arr[4] = "Adrew"; arr[5] = "Martin"; var s="hello world"; document.write(arr.slice(1) + "<br />") document.write(arr.slice(2, 4) + "<br />") document.write(s.slice(1, 4) + "<br />") document.write(s.slice(-3) + "<br />")} |
结果:
John,Thomas,James,Adrew,Martin
Thomas,James
ell
rld
4、js种的原始值(undefined,null,布尔值,数字,字符串)
|
1
2
3
4
5
|
function Test() { var s = "test"; var s1="test"; if(s===s1){}//true} |
引用类型:对象,数组
|
1
2
3
4
5
6
7
8
9
10
11
12
|
function Test() { var o={x:1}; var p={x:1}; if(o===p){}//false var a=[1,2,3]; var b=[1,2,3]; if(a===b){};//false var c=[]; var d=c; b[0]=1; if(c==d){};//true} |
5、类型转化
|
1
2
3
4
5
6
7
8
9
10
11
|
//值->转成字符串->数字->布尔//undefined->"undefined"->NaN->false//null->"null"->0->false//true->"true"->1->true//false->"false"->0->false//""(空字符串)->""->0->flase//"1.2"(非空,数字)->"1.2"->1.2->true//"one"(非空,非数字)->"one"->NaN->true//0->"0"->0->false//NaN->"NaN"->NaN->flase//1->"1"->1->true |
6、运算符
1)in运算符
|
1
2
3
4
5
6
7
|
function Test( ) { var o={x:1,y:2}; //左边为字符串或可转成字符串,右边为一个对象,左边的属性存在于右边对象,则返还ok if("x" in o){}//true if("z" in o){}//false if("toString" in o){}//true,对象继承了toString方法 } |
2)instanceof
|
1
2
3
|
var d=new Data();if(d instanceof Date){}//true 左侧对象为右侧类的实例,则返回trueif(z instanceof Date){}//false |
3)delete
|
1
2
3
4
5
|
function Test( ) { var o={x:1,y:2}; delete o.x; if("x" in o){}//false } |
7、for in
|
1
2
3
4
5
6
|
function Test( ) { var o={x:1,y:2}; for(i in o){ console.log(o[i]); } } |
8、try..catch..finally
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
function Test( ) { try{ var n=Number(prompt("请输入一个正整数")); var f=factorial(n); alert(n+"!="+f); } catch(ex){ alert(ex); } finally{ aler("不管是否有异常,都显示"); }}function factorial(x) { if(x<0){ throw new Error("x不能为负数"); } if(isNaN(x)){ throw new Error("你输入的不是一个数字"); } for(var f=1;x>1;x--){ f*=x; } return f; } |
9、width语句
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function Test() { // document.forms[0].username.value = "aa"; // document.forms[0].pwd.value = "aa"; // document.forms[0].qq.value = "aa"; // document.forms[0].realname.value = "aa"; // document.forms[0].tel.value = "aa"; //简写方式 with(document.forms[0]){ username.value="aa"; pwd.value="aa"; qq.value="aa"; tel.value="aa"; realname.value="aa"; }} |
10、js里面对象设置属性和方法
|
1
2
3
|
<h2 id="title"></h2><h3 id="des"></h3><div id="price"></div> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
function Product() { this.title = "iphone8", this.price = 20, this.des = "这是最新的产品"}Product.prototype = { buy: function () { }, getDetail: function () { }};onload = function () { var pro = new Product(); document.getElementById("title").innerHTML = pro.title; document.getElementById("des").innerHTML = pro.des; document.getElementById("price").innerHTML = pro.price;} |
浙公网安备 33010602011771号