Home >Web Front-end >JS Tutorial >Understanding of object-oriented packaging objects in JS
This article mainly introduces the understanding of object-oriented packaging objects in JS. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look.
Packaging objects: Basic types have their own corresponding packaging objects (string-->String; number --> Number; Boolean -->Boolean )
Thinking: What are the basic types? What are the reference types?
var str = new String('helllo'); console.log(typeof str); //object 这个时候str就是不是基本类型,而是对象了
The above example str is an object, so it is not surprising that it has the method charAt(), but:
var str = 'helllo'; str.charAt(0);//基本类型会找到对应的包装对象类型,然后包装对象把所有的属性和方法给了基本类型,然后包装对象消失
var str = 'hello'; str.number = 10;//基本类型要添加一个属性,它就会去找对应的包装对象类型,在这个基本类型包装对象下面去创建一个number的属性, //添加完成之后,包装对象就消失了 console.log(str.number); //undefined 包装对象消失后,在调用这句话的时候,str.numbr又重新的添加了一个对象,这个对象跟上一句中的对象不是同一个对象
The difference between the above code and the following is:
String.prototype.lastValue = function() {}//这个是添加在原型上的,所以可以找到
Related recommendations:
A brief discussion on JS packaging objects
Advanced object-oriented Wrapped object
Original value and wrapped object in js
The above is the detailed content of Understanding of object-oriented packaging objects in JS. For more information, please follow other related articles on the PHP Chinese website!