您的位置:首页 > 其它

第五章 引用类型(下)

2017-07-23 19:51 274 查看

A、基本包装类

引用类型和基本包装类型的主要区别就是对象的生存期。

<script>
// 使用new 调用基本包装类型的构造函数,返回object
// 直接调用通名转型函数,返回number
var value = 25;
var num = Number(value);
alert(typeof num); //number
var object = new Number(value);
alert(typeof object); //object
</script>


a、Boolean 类型

Boolean 类型与布尔值对应的引用类型。在ECMAScript 中的用处不大,因为它会造成误解

<script>
// 布尔运算所有对象都是true,除了null
var falseObject = new Boolean(true);
var result = falseObject && true;
console.log(result); //true
</script>


b、Number 类型

Number 是与数字值对应的引用类型。与Boolean 类似,不建议实例化。

toFixed() 方法会按照指定的小数位返回值的字符串表示,范围0 到20

toExponential() 方法以指数表示法返回该数值字符串表示形式。

toPrecision() 方法指定的精度返回该数值对象的字符串表示

"use strict";
var numObj = 12345.6789;
numObj.toFixed();
console.log(numObj.toFixed()); //12346
console.log(numObj.toFixed(1)); //12345.7
console.log(numObj.toFixed(6)); //12345.678900
console.log(numObj.toExponential(1)); //1.2e+4
console.log(numObj.toExponential(6)); //234568e+4
console.log(numObj.toPrecision(1)); //1e+4
console.log(numObj.toPrecision(6)); //12345.7
</script>


String 类型

String 类型是字符串的对象包装类型

<script>
// charAt() 方法以单字符串的形式返回给定位置的那个字符
// charCodeAt() 方法返回0 到65535 之间的整数
var stringValue = "hello world";
console.log(stringValue.charAt(2)); // l
console.log(stringValue.charCodeAt(2)); // 108

// concat() 方法可以接受任意多个参数,也就可以通过它拼接任意多个字符串
// 但实践中使用更多的是加号操作符(+)
console.log(stringValue.concat("! ", "Goodbye")); //hello world! Goodbye

// slice()、substr() 和 substring() 都会返回被操作字符串的一个子字符串,而且都接收一或两个参数
// substr() 第二个参数返回的字符个数、substring() 和slice() 最后一个字符后面的位置
// 如果负数slice()、substr() 将负的第一个参数与字符串长度相加
// substr() 将第二个参数转换为0
// substring() 将所有负数值都转换为0,将较小的位置作为开始位置,较大作为结束位置
console.log(stringValue.slice(2)); //llo world
console.log(stringValue.substr(2)); //llo world
console.log(stringValue.substring(2)); //llo world
console.log(stringValue.slice(2,5)); //llo
console.log(stringValue.substr(2,5)); //llo w
console.log(stringValue.substring(2,5)); //llo
console.log(stringValue.slice(-2)); // =slice(9) =ld
console.log(stringValue.substr(-2)); //substr(9) =ld
console.log(stringValue.substring(-2)); //hello wolrd
console.log(stringValue.slice(2,-6)); //slice(2,5) = llo
console.log(stringValue.substr(2,-6)); //"" 空
console.log(stringValue.substring(2,-6)); // he

// indexOf() 和 lastIndexOf() 从一个字符串中搜索给定的子字符串,然后返回字符串的位置(如果没找到该字符串,则返回-1)
// 第二个参数表示从哪里开始搜索,如果lastIndexOf() 第二位是负数,被当作0
console.log(stringValue.indexOf("o",5)); //7
console.log(stringValue.lastIndexOf("o",5)); //4
// 例子
var str1 = "The reason we think the past is fixed once and for all is that there is a boundary condition at the beginning of time.";
var positions = new Array();
var pos = str1.indexOf("e");
while(pos > -1){
positions.push(pos);
pos = str1.indexOf("e",pos+1);
}
console.log(positions); //[2, 5, 12, 22, 35, 41, 65, 67, 98, 101, 116]

// trim() 方法创建一个字符串副本,删除前置及后缀的所有空格,然后返回结果
// trimLeft() 和trimRight() 分别删除字符串开头和末尾的字符
var stringVal = "      hello  world    ";
console.log(stringVal);//      hello  world
console.log(stringVal.trim());//hello  world

//toLowerCase()、toLocaleLowerCase()、toUpperCase() 和toLocaleUpperCase() 大小写转换
//toLocaleLowerCase()、tolocaleUpperCase() 针对特定地区,例如土耳其语
console.log(stringValue.toLowerCase()); //hello world
console.log(stringValue.toLocaleLowerCase()); //hello world
console.log(stringValue.toUpperCase()); //HELLO WORLD
console.log(stringValue.toLocaleUpperCase()); //HELLO WORLD
// 当一个字符串与一个正则表达式匹配时,match() 方法检索匹配项
// 只接受一个参数,本质上与调用RegExp 的exec() 方法相同
var text = "cat, bat, sat, fat";
var pattern = /.at/g;
var matches = text.match(pattern);
console.log(matches); // ["cat", "bat", "sat", "fat"]
console.log(text.match(222)); //null

// search() 方法执行正则表达式和String 对象之间的一个搜索匹配,如果匹配成功,返回首次匹配项的索引,否则返回-1
console.log(text.search(/at/));

// replace() 接收两个参数,第一个参数可以是RegEXp 对象或者一个字符串,第二个参数可以是一个字符串或一个函数
console.log(text.replace("at","ond")); // 匹配字符串 cond, bat, sat, fat
console.log(text.replace(/at/g,"ond($`)")); // 匹配RegExp cond(c), bond(cat, b), sond(cat, bat, s), fond(cat, bat, sat, f)

// split() 可以基于指定的分隔符将一个字符串分割成多个字符串
console.log(text.split(",")); //["cat", " bat", " sat", " fat"]
console.log(text.split(",",2)); // ["cat", " bat"]
console.log(text.split(/[^/,]+/)); //["", ",", ",", ",", ""]
</script>


单体内置对象

由ECMAScript 实现提供的、不依赖于宿主环境的对象,这些对象在ECMAScript 程序执行之前就已经存在了。

a、Global 对象

不属于仍和其他对象的属性和方法,最终都是Global 对象的属性和方法。

1、URI 编码方式

<script>
// encodeURI() 不会对本属于URL 的特殊字符串进行编码,例如冒号、正斜杠、问号井号
// encodeURIComponent() 会对它发现的任何非标准字符串进行编码
var uri = "http://www.enjoyshine.com/index hello.html#ff";
var encode =encodeURI(uri);
var encodeCom = encodeURIComponent(uri);
var decode = decodeURI(encode);
var decodeCom =decodeURIComponent(encodeCom);
console.log(encode); //http://www.enjoyshine.com/index%20hello.html#ff
console.log(encodeCom);//http%3A%2F%2Fwww.enjoyshine.com%2Findex%20hello.html%23ff
console.log(decode); //http://www.enjoyshine.com/index hello.html#ff
console.log(decodeCom); //http://www.enjoyshine.com/index hello.html#ff
</script>


eval() 方法

eval() 函数会将传入的字符串当作JavaScript 代码进行执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: