• 第五章 引用类型> 基本包装对象


     基本包装对象

    3种特殊的引用类型:Boolean, Number和String

    当读取一个基本类型的值时,后台会创建一个对应的基本包装类型的对象,能够调用一些方法来操作这些数据。

    var s1 = "some text";
    var s2 = s1.substring(2);
    console.log(s2);
    //  me text
    
    //实际上再访问s1值的时候,后台自动完成了下面一系列操作
    var s1 = new String(“some text”);
    //创建一个String类型的实例;
    var s2 = s1.sustring(2);
    //调用指定的方法
    s1 = null;
    //销毁这个实例

    引用类型跟包装类型的主要区别: 对象的生存期。

    new操作符创建的引用类型的实例: 执行流离开作用域之前保存再内存中;

    自动创建的基本包装对象:存在于代码执行的一瞬间,随后立即被销毁。

    Boolean类型 

    基本类型和引用类型的布尔值区别:

    1、typeof操作符对基本类型返回"boolean",引用类型返回“object”;

    2、instanceof对基本类型返回false,引用类型返回true;

    Number类型

    Number类型重写了valueof()、tocaleString()和toString()方法,

    valueof:返回对象表示的基本类型的数值,

    tocaleString()和toString():返回字符串形式的数值。

    String类型

    字符方法

    charAt() 和charCodeAt(): 接受一个参数,基于0的字符位置。

    var stringValue = "hello world";
    console.log(stringValue.charAt(0));
    console.log(stringValue[1])
    //可以像访数组一样使用下标来访问字符串
    // h
    // e
    
    console.log(stringValue.charCodeAt(1));
    //"101"    

    字符串操作方法:

    concat():字符串拼接,返回拼接得到的新字符串。可以接受任意多的参数。

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

    基于子字符串创建新字符串的方法:

    slice()、substr()、substring():返回被操作字符串的一个子字符串。第一个参数指定字符串的起始位置,第二个参数字符串到那结束。

    substring():第二个参数指返回的字符个数。

    返回的都是一个基本类型的字符串值,原始字符串没有任何影响。                 

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

    传递负值

    var stringValue = "hello world";
    
    console.log(stringValue.slice(-3));          
    console.log(stringValue.substring(-3));
    console.log(stringValue.substr(-3));
    
    console.log(stringValue.slice(3, -4));
    console.log(stringValue.substring(3, -4));
    console.log(stringValue.substr(3, -4));
    
    //  rld                11+(-3);
    //  hello world    substring把负值转换为0
    // rld                 11+(-3);
    // lo w               第二参数11+( -4)              
    // hel                 substring 较小的是作为开始位置
    // ""                  第二参数把负值转为0  

    字符串位置方法:

    indexOf(): lastIndexOf() 从一个字符串中搜索给定的子字符串。返回子字符串的位置。没有找到返回-1;

    indexOf(),从字符串的头部向后搜索。lastIndexOf()从后向前。

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

    可选的第二个参数,表示从字符中的那个位置开始搜索。

    var stringValue = "hello world";
    console.log(stringValue.indexOf("o", 6));         //位置从"w"开始向后搜索,忽略之前的字符
    console.log(stringValue.lastIndexOf("o", 6));     //从“空格”开始向前搜索,忽略后面的字符
    
    // 7
    // 4
    var stringValue = " Lorem ipsum dolor sit amet, consectetur adipisicing elit";
    var positions = new Array();                           //实例化一个数组 
    var pos = stringValue.indexOf("e");                    //查找字母e保存再pos函数里
    
    while(pos > 1) {                                       //pos里面有字符的时候,
      positions.push(pos);                                //把字符串推进数组
      pos = stringValue.indexOf("e", pos + 1);            //每次找到字母,都让字符串的位置+1
    }
    console.log(positions);
    
    // [4, 25, 33, 36, 53]

    trim()方法:创建一个字符串副本,删除前置及后戳的所有空格、返回结果

    var str = " hello world ";
    var trimStr = str.trim();
    console.log(str);
    console.log(trimStr);
    // "  hello world " 
    // hello world

    字符串的模式匹配方法:

    match():接受一个参数,正则或RegExp对象

    var text = "cat, bat, sat, fat";
    var pattern = /.at/;
    
    var matches = text.match(pattern);
    console.log(matches.index);
    console.log(matches[0]);
    console.log(matches.lastIndex);
    // 0
    // cat
    //undefined

    search(): 接受一个参数,字符串或RegExp对象,返回字符串中第一个匹配项的索引;未找到返回-1;

    var text = "cat, bat, sat, fat";
    var pos1 = text.search(/at/);
    console.log(pos1);
    // 1

    replace(): 替换子字符串,接受2个字参数,第一个参数可以是一个RegExp或一个字符串。第二个参数可以是一个字符串或者函数。只会替换第一个字符串,替换全部字符串需要指定全局模式;

    var text = "cat, bat, sat, fat";
    var pos2 = text.replace("at", "ond");
    var pos3 = text.replace(/at/g, "ond");
    console.log(pos2);
    console.log(pos3);
    
    // cond, bat, sat, fat
    // cond, bond, sond, fond

    split(): 用指定的字符分割字符串,接受2个参数第一个参数:可以是字符串或RegExp对象。第二个参数,指定字符串的大小。

    var  color = "red, blue, yellow, green";
    var color1 = color.split(",");
    var color2 = color.split(",", 2);
    var color3 = color.split(/[^\,]+/);
    console.log(color1);
    console.log(color2);
    console.log(color3);
    
    // ["red", " blue", " yellow", " green"]
    // ["red", " blue"]
    // ["", ",", ",", ",", ""]

    单体内置对象

    1、Global对象:不属于任何其他对象的属性和方法,最终都是它的属性和方法。再全局作用域中定义的属性和函数,都是Global对象的属性。

    evel():像一个完整的EC解析器,只接受一个参数,即要执行的JS字符串

    eval(console.log("hi"));
    //  hi

    再eval()中创建的任何变量和函数都不会被提升,因为再解析代码的时候,它们被包含再一个字符串中,它们只在eval执行时创建。

    再Web浏览器将Golbal对象作为window对象的一部分加以实现的。

    2.Math对象

  • 相关阅读:
    jdbc连接数据库报ORA-12519错误
    Open CV 七种常见阈值分割
    开博第一天
    UIWebView的使用---safri
    转义符
    UIKIT_EXTERN 的简单用法
    iOS 基础 --- tableView的使用(一)
    iOS基础 --- 如何监听一个控件(UITextField篇)
    objective-C和C --- 《位运算》的使用(一)
    assin与weak的区别
  • 原文地址:https://www.cnblogs.com/zhangbaihua/p/5564632.html
Copyright © 2020-2023  润新知