zoukankan      html  css  js  c++  java
  • JavaScript--基本包装类型(13)

    // JS为了便于操作基本类型,提供了3个特殊的引用类型:Boolean/Number和String;

    一 基本包装类型概述

     1 // 实际上,每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,从而能够调用一些方法来操作这些数据;
     2     var box = 'Mr.Lee';                   // 定义一个String字符串;    
     3     var box2 = box.substring(2);         // 截掉字符串前两位;
     4     console.log(box2);                    // 输出新字符串;=>.Lee;
     5     // 变量box是一个字符串String类型,而box.substring(2)又说明它是一个对象(只有对象才会调用方法);
     6     console.log('Mr.Lee'.substring(3));   // 直接通过字符串值来调用方法=>Lee;
     7 
     8 // 引用类型和基本包装类型的主要区别就是对象的生存期;
     9 // 自动创建的基本包装类型的对象,则只存在于一行代码的执行瞬间,然后立即被销毁;
    10 // 这意味着我们不能在运行时为基本类型值添加属性和方法;
    11     var s1 = 'some text';                // => var s1 = new String('some text');
    12     var s2 = s1.substring(5);            // => var s2 = s1.substring(5);
    13                                          // s1 = null; 销毁这个实例;后台自动执行;
     1 1.字面量写法
     2     var box = 'Mr.Lee';                  // 字面量;
     3     box.name = 'Lee';                    // 无效属性;
     4     box.age = function(){                // 无效方法;
     5         return 100;
     6     };
     7     console.log(box.substring(3));       // =>Lee;
     8     console.log(typeof box);             // =>string;
     9     console.log(box.name);               // =>undefined;
    10     console.lgo(box.age());              // =>错误;
    2.new运算符写法
        var box = new String('Mr.Lee');
        box.name = 'Lee';
        box.age = function(){
            return 100;
        };
        console.log(box.substring(3));        // =>Lee;
        console.log(typeof box);             // =>object;
        console.log(box.name);                // =>Lee;
        console.lgo(box.age());               // =>100;
    1 // 以上字面量声明和new运算符声明很好的展示了他们之间的区别;
    2 // 但是,不管是字面量还是new运算符,都可以使用它的内置方法(substring);

    二 Boolean类型

    // Boolean类型没有特定的属性或方法;

    三 Number类型

    // Number类型有一些静态属性(通过Number直接调用,而无须new运算符)和方法;

    1 1.Number对象的静态属性
    2 MAX_VALUE            表示最大数;
    3 MIN_VALUE            表示最小值;
    4 NaN                  非数值;
    5 NEGATIVE-INFINITY    负无穷大,溢出返回该值;
    6 POSITIVE_INFINITY    无穷大,溢出返回该值;
    7 prototype            原型,用于增加新属性和方法;
    1 2.Number对象的方法
    2 toString()           将数值转化为字符串,并且可以转换进制;
    3 toLocaleString()     根据本地数字格式转换字符串;
    4 toFixed()            将数字保留小数点后指定位数并转化为字符串;
    5 toExponential()      将数字以指数形式表示;
    6 toPrecision()        指数形式或点形式表示数字;

    四 String类型

    // String类型包含了三个属性和大量的可用内置方法;

    1.String对象属性

    1 length                 返回字符串的字符长度;
    2 constructor            返回创建String对象的函数;
    3 prototype              通过添加属性和方法扩展字符串定义;

    2.String对象字符方法

    1 charAt(n)             返回指定索引位置的字符;
    2 charCodeAt(n)         以Unicode编码形式返回指定索引位置的字符的编码;
    3     var box = 'Mr.Lee';
    4     console.log(box.charAt(1));                // =>r;
    5     console.log(box.charCodeAt(1));            // =>114;
    6     console.log(box[1])                        // =>r;通过数组方式截取;

    3.String对象字符串操作方法

    1 concat(str1...str2)   将字符串参数串联到调用该方法的字符串;
    2 slice(n,m)            返回字符串位置n到m之间的字符串;
    3 substring(n,m)        同上;
    4 substr(n,m)           返回字符串位置n开始的m个字符串;
    5     var box = 'Mr.Lee';
    6     console.log(box.concat('Ok!'));          // =>Mr.Lee OK!;
    7     console.log(box.slice(3));               // =>Lee;(截取从索引3开始的后面所有字符);
    8     console.log(box.substring(3,5));         // =>Le;(截取索引3到索引5的字符);

    4.String对象字符串位置方法

     1 indexOf(str,n)        从索引n开始向后搜索第一个str,并将搜索的索引值返回;
     2 lastIndexOf(str,n)    从索引n开始向前搜索第一个str,并将搜索的索引值返回;
     3     var box = 'Mr.Lee is Lee';
     4     console.log(box.indexOf('L'));          // =>3;(从前向后搜索到的第一个L的索引值是3);
     5     console.log(box.lastIndexOf('L'));      // =>10;(从后向前搜索到的第一个L的索引值是10);
     6     console.log(box.indexOf('L',5));        // =>10;(从第5个开始向后搜索到的第一个L的索引值是10);
     7     // 如果没有找到要搜索的字符串,则返回-1;
     8 
     9     // 找出全部的L;
    10     var box = 'Mr.Lee is Lee';
    11     var boxarr = [];                        // 存放L的数组;
    12     var pos = box.indexOf('L');             // 获取第一个L的位置;
    13     while(pos>-1){                          // 如果位置大于-1,说明还存在L;
    14         boxarr.push(pos);                   // 将找到的索引添加到数组中;
    15         pos = box.indexOf('L',pos+1);       // 重新赋值pos目前的位置;
    16     }
    17     console.log(boxarr);                    // [3,10]
    18 
    19 // trim()方法
    20 // ECMAScript5为所有字符串定义了trim()方法;这个方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果;
    21     var str = '    hello  world   ';
    22     var trimstr = str.trim();
    23     console.log(trimstr);                   // =>hello  world;
        console.log(str);             // =>
    hello world
    24     // 由于trim()返回的是字符串的副本,所以原始字符串中的前置及后缀空格会保持不变;

    5.String对象字符串大小写转换方法

    1 toLowerCase(str)        将字符串全部转换为小写;
    2 toUpperCase(str)        将字符串全部转换为大写;
    3 toLocaleLowerCase(str)  将字符串全部转换小写,并且本地化;
    4 toLocaleLowerCase(str)  将字符串全部转为大写,并且本地化;

    6.String对象字符串的模式匹配方法

     1 // 具体使用方法在正则里介绍过;
     2 match(pattern)              返回pattern中的子串或null; // 与pattern.exec(str)相同;
     3 replace(pattern,replacement)用replacement替换pattern;
     4 search(pattern)             返回字符串中pattern开始位置;
     5 split(pattern)              返回字符串按指定pattern拆分的数组;
     6     var box = 'Mr.Lee is Lee';        
     7     var p = /L/g;                        // 开启全局的正则;
     8     console.log(box.match(p));           // =>[L,L];
     9     console.log(box.search(p));          // =>3;
    10     console.log(box.replace(p,'Q'));     // =>Mr.Qee is Qee;
    11     console.log(box.split(' '));         // =>['Mr.Lee','is','Lee'];

    7.String对象其他方法

    1  fromCharCode(ascii)         静态方法,输出Ascii码对应值;
    2  localeCompare(str1,str2)    比较两个字符串,并返回相应的值;

    8.String对象HTML方法

    1 // 通过JS生成一个html标签,用处不大;
    2     var box = "Lee";
    3     console.log(box.link('www.baidu.com'));    //<a href="www.baidu.com">Lee</a>;

    五 小结

    1 // 因为有了基本包装类型,所以JS中的基本类型值可以被当作对象来访问;
    2 // 基本类型特征:
    3 // 1.每个包装类型都映射到同名的基本类型;
    4 // 2.在读取模式下访问基本类型值时,就会创建对应的基本包装类型的一个对象,从而方便了数据操作;
    5 // 3.操作基本类型值的语句一经执行完毕,就会立即销毁新创建的包装对象;
  • 相关阅读:
    。【自学总结 2】------3ds Max 菜单
    。【自学总结 1】------3ds Max 界面
    。求推荐一个usb集线器的购买网址
    (翻译)Importing models-Models
    (翻译)Importing models-FBX Importer, Rig options
    [游戏开发-学习笔记]菜鸟慢慢飞(18)-迷宫
    [游戏开发-学习笔记]菜鸟慢慢飞(17)- c#-委托简记(匿名方法,Lambda表达式)
    [游戏开发-学习笔记]菜鸟慢慢飞(16)- Unity3D-Android插件问题集锦
    [游戏开发-学习笔记]菜鸟慢慢飞(15)- Unity3D-图片压缩
    [游戏开发-学习笔记]菜鸟慢慢飞(14)- ScrollView刷新
  • 原文地址:https://www.cnblogs.com/yizihan/p/4341194.html
Copyright © 2011-2022 走看看