小课堂【萌新】

JavaScript中的基本包装类型

分享人:汪胜

目录

1.背景介绍

2.知识剖析

3.常见问题

4.解决方案

5.编码实战

6.扩展思考

7.参考文献

8.更多讨论

1.背景介绍

为了便于操作基本类型值,ECMAScript 提供了 3 个特殊的引用类型:Boolean、Number和 String。这些类型与其他引用类型相似,但同时也具有与各自的基本类型相应的特殊行为。实际上,每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型的对象,从而能够调用一些方法来操作这些数据。

2.知识剖析

new运算符

                
                    var box = 'Mr. Lee';
                    box.name = 'Lee';//无效属性
                    box.age = function () {//无效方法
                    return 100;
                    };
                    console.log(box);//Mr. Lee
                    console.log(box.name);//undefined
                    console.log(box.age());//错误
                
                
                
               var box = new String('Mr. Lee');//new 运算符
                box.name = 'Lee';//有效属性
                box.age = function () {//有效方法
                return 100;
            };
                 console.log(box);//Mr. Lee
                 console.log(box.name);//Lee
                 console.log(box.age());//100
                
                

String、Boolean 和 Number都可以使用new操作符

String类型

创建: var stringObject = new String("text");

属性:length: 表示字符串中包含多少个字符

方法: 1.charAt(): 返回对应位置的特定字符,参数为基于0的位置。 2.charCodeAt(): 返回对应位置的字符编码,参数为基于0的位置。

                    
                    var stringValue =  new String("hello world");
                    console.log(stringValue.charAt(1)); //e
                    console.log(stringValue.charCodeAt(1)); //101
                
                

Boolean类型

                
                var Booleanobject=new Boolean(true);
                
                

强调:布尔表达式中的所有对象都会被转换为true。

Number类型

Number 是原始数值的包装对象。

            		
            		var numberobject=new Number(10);
            		
            	

Number具有的属性有最小值、最大值等。

Number 类型的方法

toString()将数值转化为字符串,并且可以转换进制

toLocaleString()根据本地数字格式转换为字符串

toFixed()将数字保留小数点后指定位数并转化为字符串

toExponential()将数字以指数形式表示,保留小数点后指定位数并转化为字符串

toPrecision()将数字以指数形式或点形式表示,保留小数点后面指定位数并转化为字符串

toFixed()方法:会按照指定的小数位返回数值的字符串表示

                    
                      var num=10;
                        console.log(num.toFixed(2));//10.00
                    
                

这里给toFixed()传入数值2,表示显示2位小数。

                    
                        var box = 1000.789;
                        console.log(box.toString());//转换为字符串,传参可以转换进制
                        console.log(box.toLocaleString());//本地形式,1,000.789
                        console.log(box.toFixed(2));//小数点保留,1000.79
                        console.log(box.toExponential());//指数形式,传参会保留小数点
                        console.log(box.toPrecision(3));//指数或点形式,传参保留小数点
                    
                

3.常见问题

如何替换子字符串的内容?

4.解决方案

使用replace()的方法

                
                    var h = "cat, bat, sat, fat";
            		console.log(h.replace("at", "ond"));
            		console.log(h.replace(/at/g, "end"));
                
            

5.编码实战

6.扩展思考

7.参考文献

一、javascript基本包装类型介绍

二、JavaScript:基本包装类型 - 萧萧弈寒 - 博客园

三、javaScript对象-基本包装类型的详解 - 心知梦圆 - 博客园

四、javascript学习——基本包装类型总结 - CSDN博客

五、js之基本包装类型 - 小小gogo - 博客园

8.更多讨论

鸣谢

感谢大家观看

By 汪胜