读《javasrcipt 高等程序设计》笔记。
ECMAScript供应了3种特别的援用范例:Boolean,Number,String。实际上,每当读取一个基础范例值时,背景就会建立一个对应的基础包装范例的对象。
Boolean范例
重写了valueOf()要领,返回基础范例值true或false;
重写了toString()要领,返回字符串”true”或”false”
【布尔表达式中的一切对象都会被转换为true】
请看栗子
var falseObject = new Boolean(false);
var result = falseObject && true;//true
var falseValue = false;
result = falseValue && true;//false
console.log(typeof falseObject); //object
console.log(typeof falseValue); //boolean
console.log(falseObject instanceof Boolean);//true
console.log(falseValue instanceOf Boolean);//false
falseObject虽然值为false,但这是一个对象,所以被转换为true。所以第一个result 为true。
基础范例与援用范例的布尔值的区分:
typeof关于基础范例返回boolean,关于援用范例返回Object
instanceOf测试Boolean对象返回true,测试基础范例返回false
【作者发起永久不要用Boolean对象,轻易形成误会】
Number范例
重写了valueOf()要领,返回对象示意的基础范例的数值
重写了toLocaleString(),toString()要领。返回字符串情势的数值。
toString()要领,可以通报一个示意基数的参数,则返回的值为该进制数值的字符串情势
var num = 10;
num.toString(); //”10″
num.toString(2); //”1010″二进制示意
num.toString(10); // “10” 十进制示意toFixed()要领,根据指定的小数位返回数值的字符串示意,可以自动舍入
var num = 10;
num.toFixed(2); // “10.00”
var num1 = 10.005;
num1.toFixed(2); // “10.01”toExponential() 返回以指数示意的数值的字符串情势,参数为效果中的小数位数
var num = 10;
num.toExponential(1); // “1.0e+1”toPrecision() 能够返回牢固大小花样,能够返回指数花样。参数示意数值的一切数字的位数【不包括指数部份】。会自动舍入挑选最正确的情势。可以表现1到21位小数。
var num = 99;
num.toPrecision(1); // “1e+2”
num.toPrecision(2); // “99”
num.toPrecision(3); // “99.0”
【仍不发起直接实例化Number对象。缘由与显现建立Boolean对象一样】
String范例
重写了valueOf()、toLocaleString()、toString(),都返回对象示意的基础字符串值。
length属性 示意字符串中包括的字符
charAt() 以单字符字符串情势返回给定位置的字符。
charCodeAt() 以单字符字符串情势返回给定位置的字符编码。
var stringValue = “hello world”;
stringValue.charAt(1); //”e”
stringValue.charCodeAt(1); // “101”
//IE8及其他主流浏览器支撑方括号体式格局接见。
stringValue[1];// “e”concat() 将一个或多个字符串拼接起来。
var stringValue = “hello “;
var result = stringValue.concat(“world”);//”hello world”
console.log(stringValue);//”hello “
//可接收恣意多个参数【更广泛做法是:运用+加号操作符】
var result1 = stringValue.concat(“world”,”!”);//”hello world!”slice() slice(start,end),截取start到end的字符串,不包括end。
substr() substr(start,n),截取从start最先的n个字符
substring() substring(start,end),截取从start到end的字符串,不包括end。
//都返回一个基础范例的字符串值,对原始字符串没有任何影响
//若没有制订第二个参数,默许将字符串长度作为完毕位置
var stringValue = “hello world”;
stringValue.slice(3); //”lo world”
stringValue.substring(3); //”lo world”
stringValue.substr(3); //”lo world”stringValue.slice(3,7); //”lo w”
stringValue.substring(3,7); //”lo w”
stringValue.substr(3,7); //”lo worl”//负数状况
//slice会将传入的负值与字符串长度相加
//substring会将一切负数转换为0
//substr会将负数的第一个参数加上字符串长度,负的第二个参数转换为0
stringValue.slice(-3);//”rld”
stringValue.substring(-3);//”hello world”
stringValue.substr(-3);//”rld”stringValue.slice(3,-4);//”lo w”
stringValue.substring(3,-4);//”hel”
stringValue.substr(3,-4);//””空字符串indexOf() 从字符串开首向后搜刮,返回子字符串的位置。没有则返回-1
lastIndexOf() 从字符串末端向后搜刮,返回子字符串的位置。没有则返回-1
//indexOf和lastIndexOf都接收第二个参数,示意从字符串中的哪一个位置最先搜刮
//indexOf则是从参数指定位置向后搜刮
//lastIndexOf则是从参数指定位置向前搜刮
var stringValue = “hello world”;
stringValue.indexOf(“o”); // 4
stringValue.indexOf(“o”,6);// 7trim() 建立字符串的副本,删除前置及后缀的一切空格
toLowerCase() 转换为小写
toLocaleLowerCase() 。转换为针对区域的小写体式格局
toUpperCase() 。转换为大写
toLocaleUpperCase() 。转换为针对区域的大写体式格局
match() 本质上与挪用RegExp的exec()要领雷同,只接收一个参数,要么是正则表达式,要么是RegExp对象。
var text = “cat, bat, sat, fat”;
var pattern = /.at/;var matches = text.match(pattern);
console.log(matches.index); // 0
console.log(matches[0]); // “cat”
console.log(pattern.lastIndex);//0
console.log(matches); // [“cat”, index: 0, input: “cat, bat, sat, fat”]search() 参数要么是正则表达式,要么是RegExp对象。返回字符串中第一个婚配项的索引。没有找到返回-1.【从字符串开首向后查找】
var text = “cat, bat, sat, fat”;
var pos = text.search(/at); //1replace() 。替代子字符串。接收两个参数,第一个可所以RegExp对象或一个字符串,第二个字符串可所以一个字符串或一个函数。
var text = “cat, bat, sat, fat”;
var result = text.replace(“at”,”ond”);//”cond, bat, sat, fat”
//若须要替代一切字符串,则须要供应一个正则表达式,并制订全局标志g
result = text.replace(/at/g,”ond”);//”cond,bond,sond,fond”//只要在一个婚配项的状况下,会向这个函数通报3个参数:形式婚配项、形式婚配项在字符串中的位置和原始字符串。
function htmlEscape(text){return text.replace(/[<>"&]/g, function(match, pos, originalText){ switch(match){ case "<": return "<"; case ">": return ">"; case "&": return "&"; case "\"": return """; } });
}
htmlEscape(“<p class = "greeting">Hello</p>”);
//”<p class = “greeting”>Hello</p>”split() 。基于制订分隔符将一个字符串分割成多个字符串
//接收可选的第二个参数,用于指定数组大小
var colorText = “red,blue,green,yellow”;
var color1 = colorText.split(“,”);//[“red”, “blue”, “green”, “yellow”]
var color2 = colorText.split(“,”,2);//[“red”, “blue”]
var color3 = colorText.split(/1+/);//[“”, “,”, “,”, “,”, “”]
//正则表达式觉得一脸懵逼,看来来日诰日要研究一下了。localCompare() 比较两个字符串。
字符串在字母表中排在参数之前,返回负数【平常是-1】
字符串相称,返回0
在参数以后,返回正数【平常是1】
var stringValue = “yellow”;
stringValue.localeCompare(“brisk”); //1
stringValue.localeCompare(“yellow”); //0
stringValue.localeCompare(“zoo”); //-1
- , ↩