- 基本包装类型:Boolean、Number、String
- 基本类型都不是对象,所以操作时创建临时的String等对象,用过销毁,因此不可以为基本类型值添加属性和方法
var s1="abc";
s1.color = "red";
alert(s1.color);//undefined
- Boolean类型:
- 请注意Boolean重写了valueof()返回true和false,toString()返回 ”true“和”false“,容易产生如下错误请注意:
var bool1 = new Boolean(false);
console.log(bool1 && true);//在布尔表达式求值时,所有对象类型转为true,因此bool1转为true,结果为true
此外typeof基本类型返回boolean,而Boolean返回object
- Number类型
- E8对于toFixed(0)方法在(-0.94,-0.5]以及[0.5,0.94)都返回0是错误的,IE9修正了
- String类型:
- charAt(),charCodeAt(),
- concat()、slice()、substr()//指定起始位置、substring()//指定截取个数请注意当传入的是负值时,slice将负值与字符创长度相加,substr降低一个参数加上字符串长度,第二个转换为0,substring将负值都转换为0(如果出现end>start的情况,substring会将两个数交换顺序) //notice :IE8及以下在处理负参数会出错,返回原字符串,IE9修复此问题
- .trim()//删除前置后缀的空格。
- .match()//模式匹配,接收参数是正则表达式或者RegExp对象
- .search()返回第一个字符串所在的索引。
- .replace(正则表达式,替换字符串)替换,记得正则表达式中要使用全局标志g,否则只替换匹配到的第一个。
- split()以固定字符切割数组
- localCompare()比较大小
- fromCharCode()
<!DOCTYPE html>
<html ng-app="ylApp">
<head lang="zh">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>javascript编程技术</title>
</head>
<body>
<script>
var s1="abc";
s1.color = "red";
console.log(s1.color);//undefined
//--------------------不同方法构造基本类型的差别
var num = 1;
console.log('num : ' + typeof num);//number
console.log(num instanceof Number); //false
var num1 = Number(2);
console.log('num1 : ' + typeof num1)//number
num1.name = "my num1";
console.log(num1.name);//undefined,因此当不用new时,创建的内容与直接赋值一样。
var num2 = new Number(3);
console.log('num2 : ' + typeof num2)//object
var num3 = new Object(5);
console.log('num3 : ' + typeof num3);//object
console.log(num3 instanceof Number); //true
var str = new Object("abcd");
console.log(str instanceof String); //true
//-------------------------------------------Boolean容易导致的错误
var bool1 = new Boolean(false);
console.log(bool1 && true);//在布尔表达式求值时,所有对象类型转为true,因此bool1转为true,结果为true
console.log('bool1 : ' + typeof bool1);//object
var bool2 = true;
console.log('bool2 : ' + typeof bool2);//boolean
//---------------------------------------------------Number类型
var myNum = new Number(100);
console.log(myNum.toString(2));//
console.log(myNum.toString(16));//
console.log(myNum.toFixed(2));//
console.log(myNum.toExponential(1)); //1.0e+2
var anotherNumber = new Number(-0.95)
console.log(anotherNumber.toFixed(0));//
console.log(anotherNumber.toExponential(1));//-9.5e-1
var otherNumber = 99;
//根据具体数值决定调用toFixed还是toExponential
console.log(otherNumber.toPrecision(1));//1e2
console.log(otherNumber.toPrecision(2));//99
console.log(otherNumber.toPrecision(3));//99.0
//----------------------------------------String类型
var myStr = new String("coding");
console.log(myStr.charAt(1));
console.log(myStr.charCodeAt(1));
console.log(myStr.concat(' is a beautiful thing'));
console.log(myStr.slice(3));
console.log(myStr.slice(3,7));
console.log(myStr.substr(3,7));//7是截取末尾的下一位置
console.log(myStr.substring(3,4))//4是截取个数
//参数是负数的情况
var value = "hello world";
console.log(value.slice(-3));//加上长度
console.log(value.substr(-3));//加上长度
console.log(value.substring(-3));//转为0
console.log(value.slice(3,-4));
console.log(value.substr(3,-4));//""转换为0
console.log(value.substring(3,-4));//第二个转为0,取从0开始3个字符,因而是hel
//位置方法:
console.log(value.indexOf('l'));
console.log(value.lastIndexOf('l'));
console.log(value.indexOf('l',6));//规定搜索起始位置
console.log(value.indexOf('h',6));//规定搜索起始位置,并不会在从头找起,到末尾未找到则返回-1
console.log(value.lastIndexOf('l',6));
//更精确的字符串替换
function html(test){
return test.replace(/[<>^&"]/g,function(match,pos,original){
switch (match){
case '>': return "gt;";
case '<': return "lt;";
case '"': return "amp;";
case '&': return "quot;";
}
})
}
console.log(html("<p class='greeting'>hello world!</p>"))
//--------------------------localCompare
var my = "yellow";
console.log(my.localeCompare('black'));
console.log(my.localeCompare('yellow'));
console.log(my.localeCompare('zzz'));
//-----------------fromCharCode
console.log(String.fromCharCode(102,103,104,105));
//---------------------split()
var colors = "red,blue,pink,yellow";
console.log(colors.split(','));
console.log(colors.split(',',2));
console.log(colors.split(/[^/,]+/g));//["", ",", ",", ",", ""],因为第一个切割和最后一个切割出现在头和尾
</script>
</body>
</html>