JavaScript language Essence vi
Year after year the wind wind blowing year after year slowly that long — Shagil Shuo
Start:
This section complements the previous section, which focused on arrays and introduced a common set of methods for arrays, 👉Check the array
This section is used to briefly describe the common set of methods used in JavaScript for data types other than arrays.
Number
number.toExponential(fractionDigits)
-
The toExponential method converts a numeric type to an exponential string
-
FractionDigits The value can be a decimal place less than 20
-
Such as:
var nums = 1024;
var trs = nums.toExponential(3);
console.log(trs); // 1.024e+3-- >1.024*10 ^ 3
Copy the code
number.toFixed(fractionDigits)
-
The toFixed method converts a numeric type to a decimal string
-
FractionDigits The value can be a decimal place less than 20
-
Such as:
var nums = Math.PI;
var trs = nums.toFixed(3);
console.log(trs); / / 3.142
Copy the code
number.toPrecision(precision)
toPrecision
The method is to convert a numeric type to a string of decimal numbersprecision
The value is less than 21The total number of figures- Such as:
var nums = Math.PI;
var trs = nums.toPrecision(3);
console.log(trs); / / 3.14
Copy the code
number.toString(radix)
toString
The method is to convert numeric types to stringsradix
The value can be less than 2 to 36. The default value is 10- Or you can write it for short
String(number)
- Such as:
var nums = 1024,
nums2 = 9,
nums3 = 125;
var trs = nums.toString(2);
var trs2 = nums2.toString(2);
var trs3 = nums2.toString(5);
console.log(trs); // 10000000000-->1*2 ^ 10
console.log(trs2); / / 1001
console.log(trs3); / / 1000
Copy the code
Function
function.apply(this.Ary , argArray)
-
The apply method calls function, passing an object that will be bound to this and an optional array as arguments
-
The apply method is used in the Apply Invocation pattern
-
Such as:
Function.prototype.fakeBind = function (that) {
var method = this,
slice = Array.prototype.slice,
args = slice.apply(arguments[1]);
return function () {
return method.apply(that, args.concat(slice.apply(arguments[0))); }; };var test = function () {
return this.value;
}.fakeBind({ value: 233 });
console.log(test()); / / 233
Copy the code
Object
object.hasOwnProperty(name)
hasOwnProperty
Methods are checks for properties on the object that called the method- if
object
Contains a name calledname
Property, this method returnstrue - Note: This method only checks the current object and does not check properties on the prototype chain
var list = {
name: "poo".gender: "boy"};Object.prototype.list = list;
var test = new Object(a);console.log(list.hasOwnProperty("name")); // true
console.log(test.hasOwnProperty("name")); // false
console.log(test.list.name); // poo
Copy the code
String
string.charAt(pos)
charAt
Method returns atstring
ä¸pos
The character at position- if
pos
Is less than 0 or not in the string length range, returns an empty string - Note:
JavaScript
There is no character type, so this method returns a string
let type = "food";
var pick = type.charAt(3); // d
/** Implementation principle */
String.prototype.fakeCharAt = function (pos) {
return this.slice(pos, pos + 1);
};
var ahh = "yaHoo";
let pick = ahh.fakeCharAt(2); // H
console.log(typeof pick); // string
Copy the code
string.charCodeAt(pos)
charCodeAt
与charAt
Similarly, what is returned isstring
ä¸pos
Character code point at position- if
pos
Is less than 0 or not in the string length rangeNaN
let type = "food";
var pick = type.charCodeAt(3); / / 100
console.log(typeof pick); // number
Copy the code
string.concat(string…)
concat
Method is used to concatenate a string and construct a new string- Note:
concat
The performance and ease of use are not as good as using the operators directly+
, so it is hardly used
let name = "iPhone ";
let add = "Plus ";
var pick = name.concat(add); // iPhone Plus
/** guess the implementation */
String.prototype.fakeConcat = function (add) {
return this + add;
};
let name = "tomato";
var xxx = name.fakeConcat(" Plus"); // tomato Plus
Copy the code
string.indexOf(searchString , position)
indexOf
Method is used to find another string within the current stringsearchString
- If found, returns the position of the matching string, otherwise returns -1
position
Is an optional parameter that specifies the start location of the search
let str = "xipengheng";
let pick1 = str.indexOf("eng"); / / 3
let pick2 = str.indexOf("ning"); // -1
let pick3 = str.indexOf("eng".4); / / 7
Copy the code
string.lastIndexOf(searchString , position)
lastIndexOf
与indexOf
Same, but it’s looking for a custom string in reverse order- If found, returns the position of the matching string, otherwise returns -1
position
Is an optional parameter that specifies the start location of the search
let str = "xipengheng";
let pick1 = str.lastIndexOf("eng"); / / 7
let pick2 = str.lastIndexOf("ning"); // -1
let pick3 = str.lastIndexOf("eng".4); / / 3
Copy the code
string.localeCompare(that)
localeCompare
The method is to compare two strings- If the current string is less than
that
Is negative, is equal to0
- Note: This method has no rules, no use, no need to learn
string.replace(searchValue , replaceValue)
replace
Methods tosearchVale
To find and usereplaceVale
replace- Note: Only the first occurrence of the search phrase is replaced, and a new string is returned
let str = "xipengheng";
let newStr = str.replace("eng"."a"); // xipaheng
Copy the code
- This method can also be combined with a regular to replace one or more characters as needed
let str = "xipengheng";
let reg = /eng/;
let regGlobal = /eng/g;
let newStr = str.replace(reg, "a"); // xipaheng
let gloStr = str.replace(regGlobal, "a"); // xipaha
Copy the code
string.search( regexp)
search
The method is similar toindexOf
Method, but it only accepts one regular object- If found, returns the position of the first matched character, otherwise returns -1
- This method automatically ignores the global identifier
g
And there is noposition
parameter
let str = "i miss you,and you ?";
let reg = /you/;
let regGlobal = /you/g;
let newStr = str.search(reg); / / 7
let gloStr = str.search(regGlobal); / / 7
Copy the code
string.slice(start,end)
slice
Methods to copystring
To construct a new string- if
start
If the parameter is negative, it willwithstring.length
add end
In order to takePosition of last character +1
let str = "0123456789"; //length=10
let newStr = str.slice(0.6); / / 012345
let erroStr = str.slice(-3); / / 789
Copy the code
string.split(separator,limit)
split
Method to take thisstring
Slice it up to create an arrayseparator
String or regular expression allowed, returns a single-character array if emptylimit
Will limit the number of segments that can be split- Note: This method is used quite frequently
let str = "abcde";
let newArr1 = str.split(); // [ 'abcde' ]
let newArr2 = str.split(""); // [ 'a', 'b', 'c', 'd', 'e']
let flagArr = str.split("".3); // [ 'a', 'b', 'c' ]
let pickArr = str.split("b"); // [ 'a', 'cde' ]
Copy the code
string.substring(start,end)
substring
The methods andslice
It does the same thing, except it doesn’t accept negative numbers- Note: It is better to use slice normally
string.toLocaleLowerCase()& string.toLocaleUpperCase()
toLocaleLowerCase
Method returns a new fullLowercase stringtoLocaleUpperCase
Method returns a new fullUppercase string- Cold knowledge:
**toLocaleUpperCase**
It’s used to deal with Turkish - Note:
i
Capital is — — >Ä°
Rather thanI
let str = "abCdE";
let upStr = str.toLocaleUpperCase(); // ABCDE
let lowerStr = str.toLocaleLowerCase(); // abcde
Copy the code
string.fromCharCode()
fromCharCode
The method is to find the corresponding character through the character encoding and concatenation- Note: This method returns a string
let char = String.fromCharCode(67.97.116); //Cat
console.log(typeof char); // string
Copy the code
Conclusion:
This section describes the set of common methods such as Function, Number, String, Object, and so on. In order to facilitate understanding, the principle of the official API implementation is simulated (the general idea is that I do not guarantee that it is, but it can be used, EMM).
You may also be interested in the following
- # JavaScript Language Essentials 1 (simple lab)
- Everything is an Object
- # JavaScript Language Essentials 3 (Core — functions)
- # JavaScript Language Essence 4 (Inheritance)
- Arrays and array method sets
- How are some of the apis implemented in # VUE3.0?
- JavaScript reads local file configuration (compatible with lower IE versions)
- # What do you ask about the front end of one year’s work experience?