Notes JavaScript - basic data types and packaging

First, what is the wrapper class

There are two data types Js - the basic data types and objects, JS here to see all data types
Providing three JS wrapper classes, may be converted to target basic data types
Here Insert Picture Description

Second, how to convert basic data types packaging

Conversion, essentially String (), Number (), Boolean () constructor are new objects by
For example

<script type="text/javascript">
	var num=new Number(3);
	num.at="sdfsd";//为num添加属性
	console.log(num.at);//输出sdfsd
</script>

note:

  • Not used in the actual development packaging basic data type objects into
  • If you do use some of the more time may bring unexpected results

Third, what is the role of packaging

The basic difference between the data type of the object: the basic data types of properties and methods do not, a subject

As two of said, packing is not used in the actual development of the basic data types into class object

Then the actual development, packaging How to:

  • Packaging with their own internal browser
  • The user can directly call the basic data type conversion for the object properties and methods

for example

<script type="text/javascript">
  var num =123;//基本数据类型数字
  
  num=num.toString();//浏览器临时使用包装类将num转换为对象,调用方法,然后销毁
  
  console.log(num);//输出123
</script>

Note: After the object is destroyed, the operation involves an object, the object will be re-created

for example:

<script type="text/javascript">
	var num =123;
    num.hello=123;//为num添加属性
	console.log(num.hello);//输出undefined
</script>

Why output undifined?

num.hello = 123 is added num attribute in fact new Number (). hello = 123 ,
this time num object is destroyed

Then run the console.log (num.hello) ;, num attribute is not a new object hello new Number () in
the output undefined

He published 198 original articles · won praise 94 · views 90000 +

Guess you like

Origin blog.csdn.net/shang_0122/article/details/104668990