JS之基本包装类型

2018-11-17  本文已影响1人  LemonnYan

基本包装类型

为了便于操作基本类型值,ECMAScript还提供 3 个特殊的引用类型: Boolean、 NumberString
每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,从而让我们能够调用一些方法来操作这些数据。

var s1 = "some text";
var s2 = s1.substring(2);

执行过程:

  • 创建 String 类型的一个实例;
  • 在实例上调用指定的方法;
  • 销毁这个实例。

相当于执行下面的方法:

var s1 = new String("some text");
var s2 = s1.substring(2);
s1 = null;

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

  • 使用 new 操作符创建的引用类型的实例,在执行流离开当前作用域之前都一直保存在内存中。
  • 自动创建的基本包装类型的对象,只存在于一行代码的执行瞬间,然后立即被销毁。

所以不能在运行时为基本类型值添加属性和方法。

var s1 = "some text";
s1.color = "red";
alert(s1.color); //undefined

1、Boolean 类型

Boolean 类型是与布尔值对应的引用类型。
var booleanObject = new Boolean(true);

基本类型与引用类型布尔值的两个区别:

  • typeof 操作符对基本类型返回 "boolean" ,对引用类型返回 "object";
  • instanceof操作符测试 Boolean 对象会返回 true ,测试基本类型的布尔值则返回 false 。

2、Number 类型

Number 是与数字值对应的引用类型。
var numberObject = new Number(10);

3、String 类型

String 类型是字符串的对象包装类型。
var stringObject = new String("hello world");

(1)字符方法

两个用于访问字符串中特定字符的方法是: charAt()charCodeAt()。这两个方法都接收一个参数,即基于 0 的字符位置。
charAt() 方法返回指定位置的字符;
charCodeAt()方法返回指定位置的字符编码。

var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"
alert(stringValue.charCodeAt(1)); // 输出"101"

(2)字符串操作方法

concat() 方法用于将一或多个字符串拼接起来,返回拼接的新字符串。

var stringValue = "hello ";
var result = stringValue.concat("world");

alert(result); //"hello world"
alert(stringValue); //"hello"

concat() 方法可以接受任意多个参数,可以拼接任意多个字符串。

var stringValue = "hello ";
var result = stringValue.concat("world", "!");

alert(result); //"hello world!"
alert(stringValue); //"hello"

基于子字符串创建新字符串的方法:slice() 、 substr()substring()。三个方法都会返回被操作字符串的一个子字符串,都接受一或两个参数。

slice()substring()的第二个参数指定子字符串最后一个字符后面的位置。
substr() 的第二个参数指定返回的字符个数。
如果没有指定第二个参数,将字符串的长度作为结束位置。

var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl"

参数是负值的情况下:

  • slice() 方法会将传入的负值与字符串的长度相加
  • substr() 方法将负的第一个参数加上字符串的长度,将负的第二个参数转换为 0
  • substring() 方法会把所有负值参数都转换为 0
var stringValue = "hello world";
alert(stringValue.slice(-3));        //"rld"(字符串长度加参数 11+(-3)=8)
alert(stringValue.substring(-3));   //"hello world"
alert(stringValue.substr(-3));      //"rld" (字符串长度加参数 11+(-3)=8)

alert(stringValue.slice(3, -4));        //"lo w"
alert(stringValue.substring(3, -4));    //"hel"
alert(stringValue.substr(3, -4));       //""

(3)字符串位置方法

查找子字符串的方法: indexOf()lastIndexOf()。返回子字符串的位置,如果没有找到则返回 -1 。

indexOf() 方法从字符串的开头向后搜索子字符串,lastIndexOf()方法从字符串的末尾向前搜索子字符串。
接收可选的第二个参数,表示从字符串中的哪个位置开始搜索。

var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7

(4) trim()方法

创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果。

var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
alert(stringValue); //" hello world "
alert(trimmedStringValue); //"hello world"

(5)字符串大小写转换方法

字符串大小写转换的方法有 4 个: toLowerCase() 、toLocaleLowerCase() 、 toUpperCase()toLocaleUpperCase()
toLocaleLowerCase()toLocaleUpperCase()方法则是针对特定地区的实现。

var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD"
alert(stringValue.toUpperCase()); //"HELLO WORLD"

alert(stringValue.toLocaleLowerCase()); //"hello world"
alert(stringValue.toLowerCase()); //"hello world"

(6)字符串的模式匹配方法

match()方法只接受一个参数,要么是一个正则表达式,要么是一个 RegExp对象。

var text = "cat, bat, sat, fat";
var pattern = /.at/;

//与 pattern.exec(text)相同
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0

search()由字符串或RegExp 对象指定的一个正则表达式。 search()方法返回字符串中第一个匹配项的索引 。如果没有找到匹配项,则返回 -1 。而且, search()方法始终是从字符串开头向后查找模式。

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
alert(pos); //1

replace()方法接受两个参数:第一个参数可以是一个 RegExp 对象或者一个字符串,第二个参数可以是一个字符串或者一个函数。

var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
alert(result); //"cond, bat, sat, fat"

result = text.replace(/at/g, "ond");
alert(result); //"cond, bond, sond, fond"
上一篇 下一篇

猜你喜欢

热点阅读