There are fewer methods for Number, with a total of 8:
toColorPart: Convert Number object to hexadecimal form with two digits
succ: Returns the next value of the current Number object, that is, the current value is added by one
times: Encapsulate a standard [0...n] loop in Ruby style
toPaddedString: Convert the current Number object to a string. If the converted string length is less than the value specified by length, use 0 to fill the remaining number of bits on the left
abs: Returns the absolute value of the current Number object.
round: Returns the integer value after the current Number object is rounded.
ceil: Returns the minimum integer value greater than or equal to the current Number object.
floor: Returns the maximum integer value less than or equal to the current Number object.
One of the important methods is toPaddedString. The Number object overrides the toString method:
(radix)
function toPaddedString(length,radix){
var string = (radix || 10);//Convert the number to the corresponding division first
return '0'.times(length - ) + string;//The times method is extended in String, repeating a character n times
}
With this method, there is a more useful extension, which is toColorPart, which can be used for color conversion in CSS:
function toColorPart() {
return (2, 16);
}
Since it is a CSS color conversion, the number must be within the range [0-255].
((10).toColorPart());//0a
There is a method succ with the same name as in String, and its function is similar. The String is added in a character table, and the Number is in the order of natural numbers.
function succ() {
return this + 1;
}
((10).succ());//11
Starting from this method, come up with a simple 0-n array
function range(){
var ret = [0];
for(var i = 0; i < this - 1; i++){
(());
}
return ret;
}
((10).range());//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Temporarily use this range function to get the times function:
function times(iterator, context){
().forEach(iterator, context);//The R() method is used in the source code
return this;
}
var s = '';
(5).times(function(item){
s += item;
});
(s);//01234
Apart from the above methods, the other method is to extend the static method of Math to the Number object [the statement is inaccurate, meaning... =. =】
function abs() {
return (this);
}
function round() {
return (this);
}
function ceil() {
return (this);
}
function floor() {
return (this);
}
toColorPart: Convert Number object to hexadecimal form with two digits
succ: Returns the next value of the current Number object, that is, the current value is added by one
times: Encapsulate a standard [0...n] loop in Ruby style
toPaddedString: Convert the current Number object to a string. If the converted string length is less than the value specified by length, use 0 to fill the remaining number of bits on the left
abs: Returns the absolute value of the current Number object.
round: Returns the integer value after the current Number object is rounded.
ceil: Returns the minimum integer value greater than or equal to the current Number object.
floor: Returns the maximum integer value less than or equal to the current Number object.
One of the important methods is toPaddedString. The Number object overrides the toString method:
(radix)
parameter | describe |
---|---|
radix | Optional. Specify the cardinality of the number, making an integer between 2 and 36. If this parameter is omitted, the cardinality 10 is used. But be aware that if the parameter is a value other than 10, the ECMAScript standard allows implementations to return any value. |
Copy the codeThe code is as follows:
function toPaddedString(length,radix){
var string = (radix || 10);//Convert the number to the corresponding division first
return '0'.times(length - ) + string;//The times method is extended in String, repeating a character n times
}
With this method, there is a more useful extension, which is toColorPart, which can be used for color conversion in CSS:
Copy the codeThe code is as follows:
function toColorPart() {
return (2, 16);
}
Since it is a CSS color conversion, the number must be within the range [0-255].
((10).toColorPart());//0a
There is a method succ with the same name as in String, and its function is similar. The String is added in a character table, and the Number is in the order of natural numbers.
Copy the codeThe code is as follows:
function succ() {
return this + 1;
}
((10).succ());//11
Starting from this method, come up with a simple 0-n array
Copy the codeThe code is as follows:
function range(){
var ret = [0];
for(var i = 0; i < this - 1; i++){
(());
}
return ret;
}
((10).range());//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Temporarily use this range function to get the times function:
Copy the codeThe code is as follows:
function times(iterator, context){
().forEach(iterator, context);//The R() method is used in the source code
return this;
}
Copy the codeThe code is as follows:
var s = '';
(5).times(function(item){
s += item;
});
(s);//01234
Apart from the above methods, the other method is to extend the static method of Math to the Number object [the statement is inaccurate, meaning... =. =】
Copy the codeThe code is as follows:
function abs() {
return (this);
}
function round() {
return (this);
}
function ceil() {
return (this);
}
function floor() {
return (this);
}