String calls the Array-related method-a bit odd
In the previous articles of the series, we talked about how to use the apply, call, and other methods to operate on another Object on an Object. Today we will learn how to call the Array-related methods on a String. In many ways, strings are represented as character arrays. Many Javascript array-related methods can also be used in the String type, but not all methods can. Take the following example: var name = "Benjamin"; // Outputs: TypeError: []. push. call (...) is read-only []. push. call (name, "is my name"); error: prompt that the string in Javascript is read-only, so any method to change the string will fail, therefore, we can exclude the push, pop, shift, unshift, and splice methods of arrays. However, there are still some array methods that can be used on strings. See the following example: var name = "Benjamin"; var res01 = []. some. call (name, function (val, index, arr) {// Outputs: String {0: "B", 1: "e", 2: "n", 3: "j", 4: "a", 5: "m", 6: "I", 7: "n", length: 8} console. log (arr); return val = "B" ;}); var res02 = []. every. call (name, function (val, index, arr) {return val = "B" ;}); var res03 = []. filter. call (name, function (val, index, arr) {return val <"E" ;}); // Outputs: trueconsole. log (res01); // Outputs: falseconsole. log (res02); // Outputs: ["B", "a"] console. log (res03); for details about the use of some, every, and filter methods, stamp: Javascript Array. prototype. some (), Javascript Array. prototype. every (), Javascript Array. prototype. filter () Is it great to operate the array method on the string, but you should note that the filter method returns an array instead of a string, and the output value of arr is a json object. But think about it in detail, this also makes sense. The call and apply methods do not change the logic of the function, but only change the operation value. However, if the filter method in the above example requires us to return a string, we can use the chained operation: var name = "Benjamin"; var res03 = []. filter. call (name, function (val, index, arr) {return val <"e ";}). join (""); // Outputs: ["B", "a"] console. log (res03); from the above, String cannot operate some methods on the Array, but how can we solve it? There is a quite simple way to solve this problem: we can first convert the string into a character array and then convert it into a string. Var name = "Benjamin", // TypeError: []. reverse. call (...) is read-only // res01 = []. reverse. call (name), res02 = []. slice. call (name ). reverse (). join (""); // Outputs: ["B", "e", "n", "j", "a", "m", "I ", "n"] console. log ([]. slice. call (name); // Outputs: ["n", "I", "m", "a", "j", "n", "e ", "B"] console. log ([]. slice. call (name ). reverse (); // console. log (res01); // Outputs: nimajneB console. log (res02); res01 If it is opened, the read-only error will also be reported. For details about the use of slice methods, see the description of MDN: slice does not modify the original array, only a new array containing some elements extracted from the original array is returned. The elements of the original array will be copied according to the following rules ("first-level deep copy" [one level deep] rules): If the element is an object reference (not an actual object ), slice will copy this object and reference it to the new array. Both Objects Reference the same object. If the referenced object changes, the changes are reflected in the new and original arrays. For strings and numbers (not String and Number objects), slice copies strings and numbers to the new array. Modifying these strings or numbers in one array does not affect the other array. If a new element is added to either of the two arrays, the other element will not be affected. Directly Using []. reverse. call fails to be converted. Instead, we use the slice Method to convert it to a string array, use the revserse Method for it, and then use the join method to convert it to a string. From the above description, do you feel that the method of using Array on String is a bit odd and strange, but it can also be very powerful. I hope this brief introduction will be helpful for your daily development. Thank you for reading this article. I hope you can correct it. If you feel this article is helpful to you, please like it!