引言
js的基本
包装类型有Number,Boolean,String.
Number
- 创建包装类:
new Number()
var num=new Number(12.66);
-
重要方法:
toFixed,toExponential,toPrecision.toFixed(n):四舍五入保留小数点后几(n)位的小数.toExponential(n):以科学计数法的方式四舍五入保留小数点后几(n)位的小数.toPrecision(n):保留数字的有效位数(n),视情况选择toFixed或者toExponential.
var num=new Number(12.66);
console.log("保留小数点后一位有效数字:"+num.toFixed(1));
console.log("科学计数法保留小数点后一位有效数字:"+num.toExponential(1));
console.log("取上述两者中合适的策略精确保留有效数字:"+num.toPrecision(1));
//打印结果
保留小数点后一位有效数字:12.7
科学计数法保留小数点后一位有效数字:1.3e+1
取上述两者中合适的策略精确保留有效数字:1e+1
Boolean
不推荐使用,布尔表达式中所有对象都会转化为true值
//正常情况
var flag=false;
console.log("正常情况:"+ (flag &&true));
//定义Boolean对象
var statue=new Boolean(false);
console.log("Boolean对象:"+(statue&&true));
//打印结果
正常情况:false
Boolean对象:true
String
- 字符串连接
concat:可传一个或多个字符串参数
//new的方式创建字符串
var str=new String("hello ");
var str1=str.concat("world");
console.log("连接1个: "+str1);
var str2=str.concat("world ","I am"," coming");
console.log("连接多个:"+str2);
//打印结果
连接1个: hello world
连接多个:hello world I am coming
- 求取指定索引的字符:
charAt
var str=new String("hello ");
console.log("位置1处的字符: "+str.charAt(1));
//打印结果
位置1处的字符: e
- 求取字符所在位置处的索引:
indexOf,lastIndexOf.
indexOf: 字符在字符串中第一个位置的索引,lastIndexOf:字符在字符串中最后一个位置的索引.
var str=new String("hello world ");
console.log("o在字符串的第一个索引: "+str.indexOf("o"));
console.log("o在字符串的最后一个索引: "+str.lastIndexOf("o"));
//打印结果
o在字符串的第一个索引: 4
o在字符串的最后一个索引: 7
indexOf(char,start):从起始位置start开始查询字符char------可求取字符在字符串中所有索引
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while (pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
console.log(positions);
//打印结果
[ 3, 24, 32, 35, 52 ]
- 求取字符串的字符串:
slice,substring,substr.
当只传一个参数index时,表示获取从index开始到字符串结束的子串.
var str="heloo worrrdd";
console.log("slice求取:"+str.slice(1));
console.log("substring求取:"+str.substring(1));
console.log("substr求取:"+str.substr(1));
//打印结果
slice求取:eloo worrrdd
substring求取:eloo worrrdd
substr求取:eloo worrrdd
当传两个参数时,slice和substring获取的子串符合[start,end)的规则,而substr(start,items)则表示获取从索引start开始共items个字符的子串,即符合[strat,end]的规则。
var str="heloo worrrdd";
console.log("slice求取:"+str.slice(1,6));
console.log("substring求取:"+str.substring(1,6));
console.log("substr求取:"+str.substr(1,6));
//打印结果
slice求取:eloo
substring求取:eloo
substr求取:eloo w
- 字符串大小写转换:
toUpperCase,toLowerCase
var str="heloo worrrdd";
var str1=str.toUpperCase();
console.log("转换为大写:"+str1);
var str2=str1.toLowerCase();
console.log("再次转换为小写:"+str2);
//打印结果
转换为大写:HELOO WORRRDD
再次转换为小写:heloo worrrdd
toLocaleUpperCase,toLocaleLowerCase是特定地区的实现,在不知道运行环境是什么时最好使用这两个方法。
- 替代字符串:
replace,传入两个参数 当传入的两个参数都是字符串时,仅会替代第一个匹配的字符串
var str="heloo worrroodd";
var str1=str.replace("oo","pp");
console.log("仅会替代第一个:"+str1);
//打印结果
仅会替代第一个:helpp worrroodd
当传入的第一个参数为正则表达式且标识为全局时,会替换所有.
var str="heloo worrroodd";
var str1=str.replace(/oo/g,"pp");
console.log("会替代所有:"+str1);
//打印结果
会替代所有:helpp worrrppdd
- 字符串分割:
split
var str="hel,oowo,rrro,odd";
var str1=str.split(",");
console.log("分割为数组:"+str1);
//打印结果
分割为数组:hel,oowo,rrro,odd
- 比较两个字符串:
localeCompare依次比较两个字符串的字符,当出现不同字符时,比较它们的ASCII码值,如果字符串大于字符参数时,返回1,等于时返回0,小于时返回-1,且子串一定小于包含它的串。
var a="abb";
var b="acb";
console.log("b<c传:"+a.localeCompare(b));
//打印结果
b<c传:-1
单体内置对象已经介绍过:
Object,Array,String,这次主要介绍:Global,Math
Global
URL编码方法:
encodeURI:采用UTF-8编码格式转化URI字符串。不会被此方法编码的字符:! @ # $ & ( ) = : / ; ? + ', 用decodeURI解码,常用。
var url="http://localhost:8080/test?a=1&b=张三";
console.log("encodeURI编码:"+encodeURI(url));
console.log("decodeURI解码:"+decodeURI(encodeURI(url)));
//打印结果
encodeURI编码:http://localhost:8080/test?a=1&b=%E5%BC%A0%E4%B8%89
decodeURI解码:http://localhost:8080/test?a=1&b=张三
encodeURIComponent:会将更多的特殊字符进行编码,一般用于传递参数的编码,用decodeURIComponent解码。
var url="http://localhost:8080/test?a=1&b=张三";
//会将:/等特殊字符编码
console.log("encodeURIComponent编码:"+encodeURIComponent(url));
var baseUrl="http://localhost:8080/test?person="
var param={name:"李四",age:25};
var urlComponent=encodeURIComponent(JSON.stringify(param));
console.log(decodeURIComponent(urlComponent));
console.log("对参数编码:"+(baseUrl+urlComponent));
var person=JSON.parse(decodeURIComponent(urlComponent));
console.log("对参数解码:"+person.name);
//打印结果
encodeURIComponent编码:http%3A%2F%2Flocalhost%3A8080%2Ftest%3Fa%3D1%26b%3D%E5%BC%A0%E4%B8%89
{"name":"李四","age":25}
对参数编码:http://localhost:8080/test?person=%7B%22name%22%3A%22%E6%9D%8E%E5%9B%9B%22%2C%22age%22%3A25%7D
对参数解码:李四
总结:vue的this.$router.push()传入一个对象{path:value1,query:value2},如果value2是对象可按上述方式编码解码
- eval方法:可
执行传入的字符串语句,不安全
function add(param){
var a=2;
var b=3;
console.log(eval(param));
}
var param="a+b";
add(param);
var param="a*b";
add(param);
//打印结果
5
6
- window对象:功能之一获取
global对象
var global = function(){
return this;
}();
console.log(global.clearTimeout);
//打印结果
[Function: clearTimeout]
Math
random函数产生[0,1)之间的随机数
var arr=[];
for(i=0;i<4;i++){
arr[i]=Math.random();
}
console.log(arr);
//打印结果
[
0.19526744100081617,
0.009881971462846195,
0.9534026120831793,
0.6306271962332464
]
floor向下圆整
console.log(Math.floor(12.6));
//打印结果
12
ceil向下圆整
console.log(Math.ceil(12.3));
//打印结果
13
round执行标准的四舍五入
console.log(Math.round(12.6));
console.log(Math.round(12.3));
//打印结果
13
12
- 常用组合
var arr=["zhang","san","li","si","wang","er"];
//特定两数随机生成一个数
function randomSpecial(lowerValue,upperValue){
var choice=upperValue-lowerValue+1;
return Math.floor(Math.random()*choice+lowerValue);
}
console.log(arr[randomSpecial(0,arr.length-1)]);
//第一次
er
//第二次
si