JavaScript高级程序设计读书分享之5章——基本引用类型

JavaScript高级程序设计(第4版)读书分享笔记记录

适用于刚入门前端的同志

ECMAScript 变量可以包含两种不同类型的数据:原始值和引用值。

引用值(或者对象)是某个特定引用类型的实例。在 ECMAScript 中,引用类型是把数据和功能组
织到一起的结构。

Date

菜鸟教程可以查看具体Date类型的属性和方法

要创建日期对象,就使用 new 操作符来调用 Date 构造函数:
let now = new Date();
  • ECMAScript 还提供了 Date.now()方法,返回表示方法执行时日期和时间的毫秒数
// 起始时间
let start = Date.now();
  • Date 类型的 valueOf()方法根本就不返回字符串,这个方法被重写后返回的是日期的毫秒
var d=new Date();
var n=d.valueOf();
  • getTime()返回日期的毫秒表示;与 valueOf()相同
var d = new Date();
var n = d.getTime();

RegExp

ECMAScript 通过 RegExp 类型支持正则表达式。

原始值包装类型

为了方便操作原始值, ECMAScript 提供了 3 种特殊的引用类型: Boolean Number String

Boolean

JavaScript Boolean 对象 | 菜鸟教程

Number

JavaScript Number 对象 | 菜鸟教程

  • toFixed()方法返回包含指定小数点位数的数值字符串
let num = 10; 
console.log(num.toFixed(2)); // "10.00"
  • isInteger()方法与安全整数,ES6 新增了 Number.isInteger()方法,用于辨别一个数值是否保存为整数
console.log(Number.isInteger(1)); // true 
console.log(Number.isInteger(1.00)); // true 
console.log(Number.isInteger(1.01)); // false

String

JavaScript String 对象 | 菜鸟教程

  • charAt()方法返回给定索引位置的字符,由传给方法的整数参数指定
let message = "abcde"; 
console.log(message.charAt(2)); // "c"
  • concat(),用于将一个或多个字符串拼接成一个新字符串
let stringValue = "hello "; 
let result = stringValue.concat("world"); 
console.log(result); // "hello world" 
console.log(stringValue); // "hello"
  • ECMAScript 提供了 3 个从字符串中提取子字符串的方法

任何情况下,省略第二个参数都意味着提取到字符串末尾

  • slice()
第一个参数表示子字符串开始的位置,第二个参数表示子字符串结束的位置
第二个参数是提取结束的位置(即该位置之前的字符会被提取出来,不包含该位置)
let stringValue = "hello world"; 
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"

  • substr()

第一个参数表示子字符串开始的位置,第二个参数表示返回的子字符串数量。
let stringValue = "hello world";
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.substr(3, 7)); // "lo worl"
  • substring()

第一个参数表示子字符串开始的位置,第二个参数表示子字符串结束的位置
第二个参数是提取结束的位置(即该位置之前的字符会被提取出来,不包含该位置)
let stringValue = "hello world";
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substring(3,7)); // "lo w"
  • 字符串位置方法
indexOf() 和 lastIndexOf(),这两个方法从字符 串中搜索传入的字符串,并返回位置( 如果没找到,则返回-1)。
let stringValue = "hello world"; 
console.log(stringValue.indexOf("o")); // 4 
console.log(stringValue.lastIndexOf("o")); // 7
这两个方法都可以接收可选的第二个参数,表示开始搜索的位置。
let stringValue = "hello world"; 
console.log(stringValue.indexOf("o", 6)); // 7 
console.log(stringValue.lastIndexOf("o", 6)); // 4
  • 字符串包含方法
ECMAScript 6 增加了 3 个用于判断字符串中是否包含另一个字符串的方法: startsWith()
endsWith() includes()
let message = "foobarbaz";
console.log(message.includes("bar")); // true 
console.log(message.includes("qux")); // false
  • trim()方法
ECMAScript 在所有字符串上都提供了 trim() 方法。这个方法会创建字符串的一个副本,删除前、
后所有空格符,再返回结果。
let stringValue = " hello world "; 
let trimmedStringValue = stringValue.trim(); 
console.log(stringValue); // " hello world " 
console.log(trimmedStringValue); // "hello world"
  • 字符串大小写转换
toLowerCase() toLocaleLowerCase()、toUpperCase() toLocaleUpperCase()
let 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"

单例内置对象

Math

ECMAScript 提供了 Math 对象作为保存数学公式、信息和计算的地方

min()max()方法

let max = Math.max(3, 54, 32, 16); 
console.log(max); // 54 
let min = Math.min(3, 54, 32, 16); 
console.log(min); // 3

//要知道数组中的最大值和最小值,可以像下面这样使用扩展操作符
let values = [1, 2, 3, 4, 5, 6, 7, 8]; 
let max = Math.max(...val);

舍入方法

  • Math.ceil()方法始终向上舍入为最接近的整数。
  • Math.floor()方法始终向下舍入为最接近的整数。
  • Math.round()方法执行四舍五入。
  • Math.fround()方法返回数值最接近的单精
console.log(Math.ceil(25.9)); // 26 
console.log(Math.ceil(25.5)); // 26 
console.log(Math.ceil(25.1)); // 26 
console.log(Math.round(25.9)); // 26 
console.log(Math.round(25.5)); // 26 
console.log(Math.round(25.1)); // 25 
console.log(Math.fround(0.4)); // 0.4000000059604645 
console.log(Math.fround(0.5)); // 0.5 
console.log(Math.fround(25.9)); // 25.899999618530273 
console.log(Math.floor(25.9)); // 25 
console.log(Math.floor(25.5)); // 25 
console.log(Math.floor(25.1)); // 25

random()方法

Math.random() 方法返回一个 0~1 范围内的随机数,其中包含 0 但不包含 1
可以基于如下公式使用 Math.random() 从一组整数中随机选择一个数:
function selectFrom(lowerValue, upperValue) { 
 let choices = upperValue - lowerValue + 1;  //通过将这两个值相减再加 1 得到可选总数
 return Math.floor(Math.random() * choices + lowerValue); 
}

a~b 范围内的值,其中包含 a 和 b

猜你喜欢

转载自blog.csdn.net/weixin_42307283/article/details/129161488