JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String、Math、Array、Date、RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的。

字符串对象
字符串对象创建
字符串创建(两种方式)
① 变量 = “字符串”
② 字串对象名称 = new String (字符串)
1 2
| var str1="hello world"; var str1= new String("hello word");
|
上面创建的字符串是一个字符串对象
1
| String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o", 5: " ", 6: "w", 7: "o", 8: "r", 9: "l", 10: "d", length: 11, [[PrimitiveValue]]: "hello world"}0: "h"1: "e"2: "l"3: "l"4: "o"5: " "6: "w"7: "o"8: "r"9: "l"10: "d"length: 11__proto__: String[[PrimitiveValue]]: "hello world"
|
字符串对象的属性和函数
字符串对象的属性和函数
1 2 3 4
| x.length ----获取字符串的长度 x.toLowerCase() ----转为小写 x.toUpperCase() ----转为大写 x.trim() ----去除字符串两边空格
|
字符串查询方法
x.charAt(index) ----str1.charAt(index);----获取指定位置字符,其中index为要获取的字符索引
x.indexOf(findstr,index)----查询字符串位置 index是查询的初始位置
x.lastIndexOf(findstr) –查询的是字符串的索引位置
x.match(regexp) ----match返回匹配字符串的数组,如果没有匹配则返回null
- x.search(regexp) ----search返回匹配字符串的首字符位置索引
1 2 3 4 5
| var str1="welcome to the world of JS!"; var str2=str1.match("world"); var str3=str1.search("world"); alert(str2[0]); alert(str3);
|
字符串处理方法
截断:
substr(位置,长度)
substring(开始位置,结束位置) 顾头不顾尾
slice(start,结束位置) 顾头不顾尾 切片操作
- x.substr(start, length) ----start表示开始位置,length表示截取长度
x.substring(start, end) ----end是结束位置
x.slice(start, end) ----切片操作字符串
- x.replace(findstr,tostr) ---- 字符串替换
- x.split(); ----分割字符串
- x.concat(addstr) ---- 拼接字符串
1 2 3 4 5 6 7 8 9 10 11 12 13
| var str1="abcdefgh"; var str2=str1.slice(2,4); var str3=str1.slice(4); var str4=str1.slice(2,-1); var str5=str1.slice(-3,-1); alert(str2); alert(str3); alert(str4); alert(str5);
|
1 2 3
| var str1="一,二,三,四,五,六,日"; var strArray=str1.split(","); console.log(strArray)
|
数组对象
创建方式
创建方式1:
var arrname = [元素0,元素1,….]; // var arr=[1,2,3];
创建方式2:
var arrname = new Array(元素0,元素1,….); // var test=new Array(100,”a”,true);
数组对象的属性和方法
- join方法 x.join(bystr) ----将数组元素拼接成字符串
1 2 3
| var arr1=[1, 2, 3, 4, 5, 6, 7]; var str1=arr1.join("-"); alert(str1);
|
数据增加元素
1 2 3 4 5 6
| console.log(arr1.join("==")); console.log(arr1.concat(1,2,3)); console.log(arr1.length); console.log(arr1.concat([1,2,3])); console.log(arr1.length); console.log(arr1.toString());
|
数组排序
倒序
1 2 3
| var arr1 =[54,32,45,100]; arr1.reverse() console.log(arr1);
|
1 2
| arr1.sort(); console.log(arr1);
|
//自定义一个数字的大小排序
1 2 3 4
| function intsort(a,b){ return a-b; } console.log(arr1.sort(intsort));
|
数组的切片
1 2 3
| var arr1 =[54,32,45,100,500,55]; console.log(arr1.slice(1,4)); console.log(arr1)
|
结果:
[32, 45, 100] 这是切出来的
数组的删除
splice
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| var a = [1,2,3,4,5,6,7,8]; a.splice(1,2); alert(a.toString()); a.splice(1,1); alert(a.toString()); a.splice(1,0,2,3); alert(a.toString());
|
遍历数组
1 2 3 4 5 6 7
| var arr1 = [111,222,333]; for (i=0;i<arr1.length;i++){ console.log(arr1[i]) }
|
数组的push和pop shift unshift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| var arr1=[1,2,3]; arr1.push(99); console.log(arr1); arr1.pop(); console.log(arr1); var arr1=[1,2,3]; arr1.unshift(123132); console.log(arr1); arr1.shift(); console.log(arr1);
|
都是后进先出的方法,不同在于unshift压栈的时候放在最前面
自定义的字典对象
key的引号可有可无,最后都会去掉,value字符串的时候双引号
//json实际就是Javascript的对象
容错率高,不能
Date对象
打印具体的格式的时间
- 获取日期和时间
- getDate() 获取日
- getDay () 获取星期
- getMonth () 获取月(0-11)
- getFullYear () 获取完整年份
- getYear () 获取年
- getHours () 获取小时
- getMinutes () 获取分钟
- getSeconds () 获取秒
- getMilliseconds () 获取毫秒
- getTime () 返回累计毫秒数(从1970/1/1午夜)
Date对象的方法—设置日期和时间n
创建时间对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| var nowd1=new Date(); alert(nowd1.toLocaleString( )); var nowd2=new Date("2004/3/20 11:12"); alert(nowd2.toLocaleString( )); var nowd3=new Date("04/03/20 11:12"); alert(nowd3.toLocaleString( )); var nowd3=new Date(5000); alert(nowd3.toLocaleString( )); alert(nowd3.toUTCString()); var nowd4=new Date(2004,2,20,11,12,0,300); alert(nowd4.toLocaleString( ));
|
小练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| var date = new Date(); var year=date.getFullYear(); var month=date.getMonth()+1; var day = date.getDate(); var hour=date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); var week = date.getDay(); console.log(year+"-"+month+"-"+day+" "+hour+":"+minutes+":"+seconds+" "+parseweek(week)); function parseweek(week) { var arry =["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; return arry[week]; }
|
结果:
2017-5-24 22:39:31 星期三
Math对象
- abs(x) 返回数的绝对值。
- exp(x) 返回 e 的指数。
- floor(x)对数进行下舍入。
- log(x) 返回数的自然对数(底为e)。
- max(x,y) 返回 x 和 y 中的最高值。
- min(x,y) 返回 x 和 y 中的最低值。
- pow(x,y) 返回 x 的 y 次幂。
- random() 返回 0 ~ 1 之间的随机数。
- round(x) 把数四舍五入为最接近的整数。
- sin(x) 返回数的正弦。
- sqrt(x) 返回数的平方根。
- tan(x) 返回角的正切。
Math.random是取的0-1之间的数
*100
1-100之间的随机数
1 2 3 4
| var num = Math.random(); num=num*100; num=Math.round(num); console.log(num);
|
function对象(重点)
function实际就是类
函数的定义
1 2 3
| function 函数名 (参数){<br> 函数体; return 返回值; }
|
用 Function 类直接创建函数的语法如下:
var 函数名 = new Function(“参数1”,”参数n”,”function_body”);
注意:js的函数加载执行与python不同,它是整体加载完才会执行,所以执行函数放在函数声明上面或下面都可以:
函数function 对象属性
函数的调用
1 2 3 4 5 6 7 8
| -------------------面试题----------- function a(a,b){ alert(a+b); } var a=1; var b=2; a(a,b)
|
这个会报错,因为a已经被覆盖了,后面不能当做函数进行调用了
函数的内置对象argums
使用arguments可以是直接使用函数的属性
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 26 27 28 29 30 31 32 33
| function add(a,b){ console.log(a+b); console.log(arguments.length); console.log(arguments); } add(1,2) ------------------arguments的用处1 ------------------ function nxAdd(){ var result=0; for (var num in arguments){ result+=arguments[num] } alert(result) } nxAdd(1,2,3,4,5) function f(a,b,c){ if (arguments.length!=3){ throw new Error("function f called with "+arguments.length+" arguments,but it just need 3 arguments") } else { alert("success!") } } f(1,2,3,4,5)
|
匿名函数
先想一下Python的lambd的使用
(lambda x,y:x+y)(1,2)
只使用一次,省内存
js版本的匿名函数
1 2 3
| (function () { console.log("hello") })()
|
1 2 3
| (function (name) { console.log(name) })("haha")
|
参考内容:
http://www.cnblogs.com/yuanchenqi/articles/6893904.html