Basic wrapper types in JavaScript

1. Background Introduction

In order to facilitate the manipulation of basic type values, ECMAScript provides three special reference types: Boolean, Number and String. These types are similar to other reference types, but also have special behaviors corresponding to their respective base types. In fact, whenever a basic type value is read, an object of the corresponding basic wrapper type is created in the background, so that some methods can be called to manipulate the data.

2. Knowledge analysis

new operator

var box = 'Mr. Lee';
box.name = 'Lee';//Invalid property
box.age = function () {//Invalid method
return 100;
};
console.log(box);//Mr. Lee
console.log(box.name);//undefined

console.log(box.age());//Error


var box = new String('Mr. Lee');//new operator
box.name = 'Lee';//valid property
box.age = function () {//valid method
return 100;
};
console.log (box);//Mr. Lee
console.log(box.name);//Lee
console.log(box.age());//100
String, Boolean and Number can all be created using the new operator String


type :

var stringObject = new String("text");
Attribute: length: Indicates how many characters are contained in the string
Methods : 1.charAt(): Returns the specific character at the corresponding position, the parameter is a 0-based position.
           2.charCodeAt(): Returns the character code of the corresponding position, the parameter is a 0-based position.



var stringValue =  new String("hello world");

console.log(stringValue.charAt(1)); //e

console.log(stringValue.charCodeAt(1)); //101


Boolean type

var Booleanobject=new Boolean(true);

Emphasis: All objects in Boolean expressions will be converted to true.


Number type
Number is a wrapper object for primitive values.


var numberobject=new Number(10);


Number has the properties of minimum, maximum and so on.

The method toString() of the Number type
converts the numeric value to a string, and can convert the base
toLocaleString() to a string according to the local number format
toFixed() retains the specified number of digits after the decimal point and converts it to a string
toExponential() will The number is expressed in exponential form, the specified number of digits after the decimal point are reserved and converted to a string
toPrecision() The number is expressed in exponential form or point form, the specified number of digits after the decimal point are reserved and converted into a string
toFixed() method: will be as specified The string representation of the return value with decimal places

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


Here, the value 2 is passed to toFixed(), which means that 2 decimal places are displayed.


var box = 1000.789;

console.log(box.toString());//Convert to string, pass parameters can be converted into hexadecimal

console.log(box.toLocaleString());//local form, 1,000.789

console.log(box.toFixed(2));//The decimal point is reserved, 1000.79

console.log(box.toExponential());//Exponential form, the decimal point is retained when passing the participants

console.log(box.toPrecision(3));//Exponential or point form, pass the parameter to retain the decimal point


3. FAQ


How to replace the content of a substring?


4. Solutions


Using the replace() method

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


5. Coding practice


6. Expansion thinking


7. References


1. Introduction to javascript basic packaging types


2. JavaScript: basic packaging types- Xiao Xiao Yihan- Blog Garden


3. javaScript objects - detailed explanation of basic packaging types- Xinzhimengyuan- Blog Garden


4. javascript learning - summary


of basic packaging types - CSDN blog 5. Basic packaging types of js - small gogo - Blog Park


8. More discussion



Question one

What is the relationship between primitive types and reference types in javascript?

answer:

It is an independent relationship. Variables in js are classified into 1. Basic type: The content of the variable is stored in the local of the variable. 2. Reference type: The content of the variable is not stored locally in the variable but is stored elsewhere in the memory, and the value of the address of the memory is stored in the variable.


Question two

Why doesn't the Boolean type have specific properties or methods?

Answer: The object constructed by Boolean has no specific properties or methods, and all objects in the Boolean expression will be converted to true.


Question three

How are reference types different from primitive wrapper types?

answer:

in the life cycle of the object.

Instances of reference types created with the new keyword exist in the current scope. The object automatically created by the basic packaging type exists when the object is called, and after the call is completed, it is destroyed and no longer exists.

The Object() constructor can return an instance of the basic wrapper type according to the type of the parameter you pass in. If you pass in a string, it will return an instance of type String; if you pass in a number, it will return an instance of type Number; if you pass in a boolean value, it will return an instance of type Boolean. For example:

var obj = new Object("hello,1024idea");
console.log(obj instanceof String);// true
Note: The effect of the constructor created with the new keyword is different from that of the transformation function with the same name. For example:

// The difference between the return value of the constructor and the transformation function
var str = "1024";

var strObj = new String(str);
console.log(typeof strObj);//object

var strNum = Number(str);
console. log(typeof strNum);//number
In this example, the variable strObj holds the object type, and the variable strNum holds the number type.

In general, it is not recommended to manually create objects of basic wrapper types, because this often causes the problem of not knowing whether you are dealing with basic wrapper types or reference types.



PPT

video

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528713&siteId=291194637