JavaScript高级程序设计读书笔记(第五章)(三)

引用类型

基本包装类型

ECMAScript 还提供了3 个特殊的引用类型:Boolean、Number 和String。
引用类型与基本包装类型的主要区别就是对象的生存期。使用new 操作符创建的引用类型的实例,
在执行流离开当前作用域之前都一直保存在内存中。而自动创建的基本包装类型的对象,则只存在于一行代码的执行瞬间,然后立即被销毁。这意味着我们不能在运行时为基本类型值添加属性和方法。

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

Object 构造函数也会像工厂方法一样,根据传入值的类型返回相应基本包装类型的实例。

var obj = new Object("some text");
alert(obj instanceof String); //true

值得注意的是,使用new 调用基本包装类型的构造函数,与直接调用同名的转型函数是不一样的。new 出来的变量其typeof检测的类型值是Object,而直接创建的基本数据类型对象会检测出string,number或者boolean。

Number类型:

  • toFixed()方法:toFixed()方法会按照指定的小数位返回数值的字符串表示:
var num = 10;
alert(num.toFixed(2)); //"10.00"

toFixed()方法可以表示带有0 到20 个小数位的数值。

String类型

  • charAt()方法:接收一个参数,即基于0 的字符位置。charAt()方法以单字符字符串的形式返回给定位置的那个字符
var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"
  • concat()方法:字符串连接,,concat()方法可以接受任意多个参数,也就是说可以通过它拼接任意多个字符串。
var stringValue = "hello ";
var result = stringValue.concat("world");
alert(result); //"hello world"
var stringValue = "hello ";
var result = stringValue.concat("world", "!");
alert(result); //"hello world!"

在实践中更多的使用’+’操作符拼接字符串。

  • slice()方法和substring()方法:截取字符串,传入两个参数,分别是截取字符串的开始和结束位置——不包含结束位置的字符,也可以只指定一个参数,表示截取此位置之后的所有字符。不会修改字符串本身的值,只是返回截取结果:
var str = 'text';
str.slice(1,2);//e
str.slice(1);//ext
str.substring(1,2);//e
str.substring(1);//ext
  • substr():截取字符串,接收两个参数,第一个是起始位置,第二个是截取字符的个数。

在处理负数参数时,他们的三个方法的表现不同,substr()方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0。slice则将两个负数都加上字符串的长度,得到两个新参数。substring()方法会把所有负值参数都转换为0。

var stringValue = "hello world";
alert(stringValue.slice(-3)); //"rld" = slice(8)
alert(stringValue.substring(-3)); //"hello world" = substring(0)
alert(stringValue.substr(-3)); //"rld" = substr(8)
alert(stringValue.slice(3, -4)); //"lo w" = slice(3, 7)
alert(stringValue.substring(3, -4)); //"hel" = substring(3, 0) = substring(03)
alert(stringValue.substr(3, -4)); //""(空字符串)=substr(3, 0)
  • indexOf和lastIndexOf()方法:这两个方法都是从一个字符串中搜索给定的子字符串,然后返子字符串的位置,lastIndexOf()是从后往前搜索字符。
var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7

这两个方法都可以接收可选的第二个参数,表示从字符串中的哪个位置开始搜索:

var stringValue = "hello world";
alert(stringValue.indexOf("o", 6)); //7
alert(stringValue.lastIndexOf("o", 6)); //4
  • trim()方法:去除字符串两端的空格。
var str= '  hello  ';
alert(str.trim());//'hello'
  • toLowerCase()、toLocaleLowerCase()、toUpperCase()和toLocaleUpperCase():转换大小写方法。

  • split():这个方法可以基于指定的分隔符将一个字符串分割成
    多个子字符串,并将结果放在一个数组中。此方法接收两个参数:第一个参数时分隔符,分隔符可以是字符串,也可以是一个RegExp 对象,第二个参数是数组的长度:

var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2); //["red", "blue"]

单体内置对象

Global对象

不属于任何其他对象的属性和方法,最终都是Global对象的属性和方法。所有在全局作用域中定义的属性和函数,都是Global 对象的属性。,诸如isNaN()、isFinite()、parseInt()以及parseFloat(),实际上全都是Global对象的方法。

  • Global 对象的encodeURI()和encodeURIComponent()方法可以对URI(Uniform ResourceIdentifiers,通用资源标识符)进行编码,以便发送给浏览器。
var uri = ' 百度'
console.log(encodeURI(uri));
// %20%E7%99%BE%E5%BA%A6,encodeURI对空格和中文字符进行了编码

解码使用decodeURI()和decodeURIComponent()。

  • eval()方法:
    方法就像是一个完整的ECMAScript 解析器,它只接受一个参数,即要执行的ECMAScript (或JavaScript)字符串。eval内执行的代码和在全局执行结果一样。
eval("alert('hi')") ==>alert("hi");
var msg = "hello world";
eval("alert(msg)"); //"hello world"
eval("function sayHi() { alert('hi'); }");
sayHi();
var global = function(){
    return this;
}();

以上代码创建了一个立即调用的函数表达式,返回了Global对象。

Math对象

  • min()和max()方法:取得一组数值中的最小值和最大值。
var max = Math.max(3, 54, 32, 16);
alert(max); //54
var min = Math.min(3, 54, 32, 16);
alert(min); //3

要找到数组中的最大或最小值,可以像下面这样使用apply()方法。

var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);
  • Math.ceil()、Math.floor()和Math.round():将小数值舍入为整数。

    • Math.ceil()向上取整;
    • Math.floor()向下取整;
    • Math.round()四舍五入;
  • Math.random()方法:返回大于等于0 小于1 的一个随机数。
    使用方法举例:

function selectFrom(lowerValue, upperValue) {
    var choices = upperValue - lowerValue + 1;
    return Math.floor(Math.random() * choices + lowerValue);
}
var num = selectFrom(2, 10);
alert(num); // 介于 2 和10 之间(包括 2 和 10)的一个数值

上例中,可以自定义取值范围,从而创造取值范围中的随机数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员青戈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值