Summary of creating objects in JS, Summary of creating objects in JS
In JS, we often use objects, including numbers, arrays, strings, dates, and so on. objects are nothing more than data of attributes and methods. Objects can be created using Constructors (new + common functions,
Var num = new Number (value );
Var num = Number (value); value indicates the value of the object.
Although sometimes we do not need to do this, we can directly call the corresponding function to achieve the goal, such as Number (), String (), Array (), Boolean (), Date () and so on.
You can do this directly:
Var num = Number (value)
In an object, the attribute is the attribute of the constructor, rather than the attribute of the object defined by the constructor.
For numeric objects, its attributes include:
Attribute |
Description |
Constructor |
Returns a reference to the Number function that creates this object. |
MAX_VALUE |
The maximum number that can be expressed. |
MIN_VALUE |
The smallest number that can be expressed. |
NaN |
Non-numeric value. |
NEGATIVE_INFINITY |
Negative infinity. This value is returned when overflow occurs. |
POSITIVE_INFINITY |
Positive infinity. This value is returned when overflow occurs. |
Prototype |
Allows you to add attributes and methods to objects. |
For example
Var num = Number (). MIN VALUE
But this cannot be used.
Var num = Number (1 );
Var mxnum = num. MAX VALUE
Similarly, a method is an object method, rather than a constructor method, that is, a method cannot be called by a constructor to execute an action. Instead, a method should be called through an object.
For numeric objects, the methods include:
Method |
Description |
ToString |
Converts a number to a string and uses the specified base number. |
ToLocaleString |
Converts a number to a string in the format of a local number. |
ToFixed |
Converts a number to a string. The result is a number of the specified digits after the decimal point. |
ToExponential |
Converts an object value to an exponential notation. |
ToPrecision |
Format the number to the specified length. |
ValueOf |
Returns the basic numeric value of A Number object. |
For example
Var num = Number (123 );
Var OXnum = num. toString (16); // convert the value of the num object to a hexadecimal number.
Or
Var num = 123;
Var OXnum = num. toString (16); // in this case, JS converts num into a Number object and then calls its method.
This is also true for other types of objects and will not be repeated.