JS中的引用类型数据

 最近在翻看JS高程三时候,才发现自己对js的理解是多么的微不足道,虽然在日常的工作中并不会关系到底层什么的,还是以业务为主,加班深夜也是家常便饭,现在还是有点空的,闲来无事,翻翻书。

    闲话少说,JS中有7种的数据类型Undefined 、Null 、Boolean 、Number 、String  Symbol .Object

    这七种数据类型又分为基本的原始类型,和引用类型

    今天主要来写一下引用类型

    而引用类型又分为两种,感觉有点绕,就这集中数据来绕来绕去的 先来看看基本的引用类型

    1.Date  在工作中用的挺多的,获取当前时间什么的,下单倒计时什么的,这里就有个坑,ios系统要注意

    2.RegExp  不多讲这个了,抽空研究一下,基本上都是用到什么搜什么

    3.原始值包装类型 String Number Boolean 用构造函数创建的 其实这三种都有继承的三个方法3个继承的方法valueOf() 、toLocaleString() 和toString() 

        主要说一下String  继承的三个方法都返回对象的原始字符串值。

        常用String的操作方法

          concat()拼接字符串 ,但是在日常中用不到,一般都是用+号拼接或者模板字符串拼接 

          从String中提取字符串的操作方法 

            1. slice() 、substr() 和substring()

                这三个方法都会但会操作后的字符串 或者截取的字符串 

                slice 和 substring 都是截取字符串,支持两个参数,第二个参数是截止的下标

                substr 的第二个参数是从第一个参数开始,截取多少位

let stringValue = "hello world";
console.log(stringValue.slice(3));       // "lo world"
console.log(stringValue.substring(3));   // "lo world"
console.log(stringValue.substr(3));      // "lo world"
console.log(stringValue.slice(3, 7));    // "lo w"
console.log(stringValue.substring(3,7)); // "lo w"
console.log(stringValue.substr(3, 7));   // "lo worl"

             

          从字符串中返回子字符串位置的方法 查看某一字符的位置 返回位置的下标

              1.indexOf() 和lastIndexOf() 

                      分别是从字符串前面开始搜索 和从字符串垢面开始搜索 而且他支持两个参数

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

  第二个参数会忽略掉初始位置之前

          解构字符串

              

let str=‘abcde’
console.log([...str]) //['a',......]

    字符串还有很多方法 转大小写等等 就不一一列举说明了

      还有另外两个单例内置对象:Global 和Math  也不说明了 

  下面在说一下另一种引用类型 集合引用类型 

  • 对象
  • 数组与定型数组
  • Map 、WeakMap 、Set 以及WeakSet 类型

    对象暂时不说了 大体说一下Array吧,平常用到array的api也挺多的

    

    1.创建数组 

        第一种方法是使用构造函数创建 也就是new Array()  

        第二种方法使用字面量的形式创建  let arr=[1,2,3]

        es6新增的方法from() 和of()

        

let arr=Array.form('tom') //['t','o','m']

        而且form 也可以对数组进行浅复制

const a1 = [1, 2, 3, 4];
const a2 = Array.from(a1);

console.log(a1);        // [1, 2, 3, 4]
alert(a1 === a2); // false

    累了 不想写了 下次有空再写一下array的api 挺多的

原文地址:https://www.cnblogs.com/klwblogs/p/14192949.html