zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Array.prototype.toString()和Array.prototype.toLocaleString()

Array prototype toString
2023-09-11 14:15:02 时间

Array.prototype.toString()

toString() 返回一个字符串,表示指定的数组及其元素。

Array 对象覆盖了 Object 的 toString 方法。对于数组对象,toString 方法返回一个字符串,该字符串由数组中的每个元素的 toString() 返回值经调用 join() 方法连接(由逗号隔开)组成。
当一个数组被作为文本值或者进行字符串连接操作时,将会自动调用其 toString 方法。
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr'];
var myVar = monthNames.toString(); // assigns "Jan,Feb,Mar,Apr" to myVar.


var i = [1, 2, 3,[1, 2, 3, [1, 2, 3]]];
i.toString()
//"1,2,3,1,2,3,1,2,3"

其实toString()就是返回了String(this)

Array.prototype.toLocaleString()

toLocaleString() 返回一个字符串表示数组中的元素。数组中的元素将使用各自的 toLocaleString 方法转成字符串,这些字符串将使用一个特定语言环境的字符串(例如一个逗号 ",")隔开。
locales 可选
带有BCP 47语言标记的字符串或字符串数组
options 可选
一个可配置属性的对象,对于数字 Number.prototype.toLocaleString(),对于日期Date.prototype.toLocaleString().
使用locales和options
数组中的元素将会使用各自的 toLocaleString 方法:

Object: Object.prototype.toLocaleString()
Number: Number.prototype.toLocaleString()
Date: Date.prototype.toLocaleString()
总是在prices数组中显示字符串和数字的货币符号:
var prices = ['¥7', 500, 8123, 12];
prices.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' });

// "¥7,¥500,¥8,123,¥12"
Polyfill
// https://tc39.github.io/ecma402/#sup-array.prototype.tolocalestring
if (!Array.prototype.toLocaleString) {
    Object.defineProperty(Array.prototype, 'toLocaleString', {
      value: function(locales, options) {
        // 1. Let O be ? ToObject(this value).
        if (this == null) {//空数组抛错误
          throw new TypeError('"this" is null or not defined');
        }
  
        var a = Object(this);//转成对象
  
        // 2. Let len be ? ToLength(? Get(A, "length")).
        var len = a.length >>> 0;//数组长度
  
        // 3. Let separator be the String value for the 
        //    list-separator String appropriate for the 
        //    host environment's current locale (this is 
        //    derived in an implementation-defined way).
        // NOTE: In this case, we will use a comma
        var separator = ',';//分隔符是逗号
  
        // 4. If len is zero, return the empty String.
        if (len === 0) {//如果长度为0,返回空字符串
          return '';
        }
  
        // 5. Let firstElement be ? Get(A, "0").
        var firstElement = a[0];//第一个数组元素
        // 6. If firstElement is undefined or null, then
        //  a.Let R be the empty String.
        // 7. Else,
        //  a. Let R be ? 
        //     ToString(? 
        //       Invoke(
        //        firstElement, 
        //        "toLocaleString", 
        //        « locales, options »
        //       )
        //     )
        var r = firstElement == null ? 
          '' : firstElement.toLocaleString(locales, options);//判断第一个元素为空情况然后计算后赋值给r
  
        // 8. Let k be 1.
        var k = 1;
  
        // 9. Repeat, while k < len
        while (k < len) {//循环数组
          // a. Let S be a String value produced by 
          //   concatenating R and separator.
          var s = r + separator;//分隔符
  
          // b. Let nextElement be ? Get(A, ToString(k)).
          var nextElement = a[k];//下一个字符
  
          // c. If nextElement is undefined or null, then
          //   i. Let R be the empty String.
          // d. Else,
          //   i. Let R be ? 
          //     ToString(? 
          //       Invoke(
          //        nextElement, 
          //        "toLocaleString", 
          //        « locales, options »
          //       )
          //     )
          r = nextElement == null ? 
            '' : nextElement.toLocaleString(locales, options);//下一个字符转换成字符串
  
          // e. Let R be a String value produced by 
          //   concatenating S and R.
          r = s + r;
  
          // f. Increase k by 1.
          k++;
        }
  
        // 10. Return R.
        return r;
      }
    });
  }