javascript基本包装类型及其操作方法

1.为了便于操作基本类型值,ECMAScript还提供了3个特殊的因哟用类型:Boolean、Number、Striung。

这些类型与其他引用类型相似,但同时也具有各自的基本类型相应的特殊行为,实际上每当读取一个基本类型值得时候,

后台就会创建一个对应的基本包装类型的对象,从而让我们能够调用一些方法来操作这些数据。

2.举例示意。

<!DOCTYPE html>
<html lang="en">

<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/style.css" rel="stylesheet">
<script src="js/vue.js"></script>
</head>

<body>
<!--基本包装类型,每当读取一个基本类型的时候,后台就会创建一个对应的基本包装类型的对象-->
<!--1.创建一个String实例,2.在实例上调用指定的方法3.销毁这个实例。-->
<script>
// 使用new操作符创建的引用类型的实例在执行到离开作用域之前会一直保存在内存中
var s1 = new String("some text"); //基本字符串转化为对象,
var s2 = s1.substring(2);
console.log(s2);

// 而自动创建的基本包类型的对象在一行代码执行瞬间然后就立即被销毁
var s1 = "some text";
s1.color = "red";
console.log(s1.color); //underfined
// 基本包装类型实例调用typeof 会返回“”object,而且所有基本包装类型都会转换为布尔值true
// 把字符串传给Object函数就会创建String的实例,
// 而传入的参数就会得到number的实例,传入的布尔值参数就会得到Boolean的实例
var obj = new Object("some text");
console.log(obj instanceof String); //true

var value = "25";
var number = Number(value); //转型函数
console.log(number); //number
var obj = new Number(value);
console.log(typeof obj); //object

// Boolean类型,其实例重写了valueof()方法,返回基本类型值true falue
// 不二表达式中所有的对象都会转换为true
var booleanObject = new Boolean(true);
var falseObject = new Boolean(false); //false会被转换为true
var result = falseObject && true;
console.log(result); //true

var falseValue = false;
result = falseValue && true;
console.log(result); //false

// Number类型
// Nunber类型也重写了valueof(),toLocaleString()和toString()方法
// value()返回对象表示的基本类型的数值,另外两个方法则返回字符串形式的数值
var nunberObject = new Number(10);
var num = 10;
console.log(num.toString());
console.log(num.toString(2));
console.log(num.toString(8));
console.log(num.toString(10));
// 将数值格式化为字符转的方法
var num = 10;
console.log(num.toFixed(2)); //10.00,带两位小数
var  num = 10.005;
console.log(num.toFixed(2)); //10.01四舍五入

// Stirng类型 字符串的对象包装类型
var stringValue = "hello world";
console.log(stringValue.length); //11
//字符方法 charAt() charCodeAt()
var stringValue = "hello world";
console.log(stringValue.charAt(1)); //e
var stringValue = "hello world";
console.log(stringValue.charCodeAt(1)); //101小写字母e的编码
console.log(stringValue[1]); //e
// 字符串操作方法concat()字符串拼接,concat()可以接受多个参数,
var stringValue = "hello ";
var result = stringValue.concat("world");
console.log(result); //hello world
console.log(stringValue); //hello

// slice() substr() substring(),接受一个或两个参数,第一个开始位置第二个结束位置。
var stringValue = "hello world";
console.log(stringValue.slice(-3)); //rld
// substring()会将负值转化为0
console.log(stringValue.substring(-3));
// 字符串位置方法indexOf(),lastIndexOf(),方向相反
// 空格也算一个字符
var stringValue = "hello world";
console.log(stringValue.indexOf("h")); //从0开始
console.log(stringValue.indexOf("o")); //4
console.log(stringValue.lastIndexOf("o")); //7

var stringValue = "hello world";
console.log(stringValue.indexOf("o", 6)); //7
console.log(stringValue.lastIndexOf("o", 6)); //4

var stringValue = "Lorem ipsum dolor sit amet,consectetur adipisicing slit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while (pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);

}
console.log(positions);
//trim()方法,删除一个字符串的副本前后所有的空格
var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
console.log(stringValue); //" hello world ";
console.log(trimmedStringValue); //hello world

//字符串大小转换方法
var stringValue = "hello world";
console.log(stringValue.toLocaleUpperCase()); //HELLO WORLD
console.log(stringValue.toUpperCase()); //HELLO WORLD
console.log(stringValue.toLocaleLowerCase()); //hello world
console.log(stringValue.toLowerCase()); //hello world

// 字符串模式匹配方法
// match()方法接收一个参数要么是一个参数要么是个正则表达式
// match()方法返回一个数组
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
//search()方法 唯一参数与match()相同
var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
console.log(pos); //1 at第一次出现的位置。

// replace()方法,接收两个函数,第一个参数可以正则对象也可以是字符串,
// 第二个函数可以是字符串也可以是一个函数
var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
console.log(result); //cond, bat, sat, fat替换,
result = text.replace(/at/g, "ond"); //全局替换
console.log(result); //cond, bond, sond, fond
var text = "cat, bat, sat, fat";
result = text.replace(/(.at)/g, "word($1)");
console.log(result); //word(cat), word(bat), word(sat), word(fat)
//split();将字符分割成字符串并放在一个数组中
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");
var colors2 = colorText.split(",", 2);
var colors3 = colorText.split(/[^\,]+/);
console.log(colors1); //["red", "blue", "green", "yellow"]
console.log(colors2); //["red", "blue"]
console.log(colors3); //["", ",", ",", ",", ""]

//localeCompare()方法,这个方法比较两个字符串,并返回结果
//1.字符串在字母表中应该排在字符产参数之前,返回负数
// 2.字符串等于字符串参数,返回
// 3.如果字符串在字母表中应该排在字符串参数之后,返回正数
var stringValue = "yellow";
console.log(stringValue.localeCompare("brick")); //1
console.log(stringValue.localeCompare("yellow")); //0
console.log(stringValue.localeCompare("zoo")); //-1
</script>
</body>

</html>
原文地址:https://www.cnblogs.com/zhengao/p/6684575.html