You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
Phương thức repeat()
xây dựng và trả về một chuỗi mới chứa số lượng nhất định bản sao chép của chuỗi được gọi tới và nối chung với nhau.
Cú pháp
str.repeat(count);
Tham số
count
- Là 0 hoặc số nguyên dương, tức là giá trị nằm trong khoảng: [0, +∞), xác định số lần lặp để tạo chuỗi mới.
Giá trị trả về
Một chuỗi mới chứa số lần sao chép (count) chuỗi đầu vào.
Ngoại lệ
RangeError
: số lần lặp phải không âm.RangeError
: số lần lặp phải nhỏ hơn vô cực và không vượt kích cỡ chuỗi tối đa.
Ví dụ
'abc'.repeat(-1); // RangeError 'abc'.repeat(0); // '' 'abc'.repeat(1); // 'abc' 'abc'.repeat(2); // 'abcabc' 'abc'.repeat(3.5); // 'abcabcabc' (tham số đếm sẽ được chuyển thành số nguyên) 'abc'.repeat(1/0); // RangeError ({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2); // 'abcabc' (repeat() is a generic method)
Polyfill
Phương thức này đã được thêm vào kỹ thuật ES6 và có thể chưa có sẵn trong tất cả các bản bổ sung JS. Tuy nhiên bạn có thể sử dụng polyfill String.prototype.repeat()
với snippet dưới đây:
if (!String.prototype.repeat) { String.prototype.repeat = function(count) { 'use strict'; if (this == null) { throw new TypeError('can\'t convert ' + this + ' to object'); } var str = '' + this; count = +count; if (count != count) { count = 0; } if (count < 0) { throw new RangeError('repeat count must be non-negative'); } if (count == Infinity) { throw new RangeError('repeat count must be less than infinity'); } count = Math.floor(count); if (str.length == 0 || count == 0) { return ''; } // Đảm bảo tham số đếm là số nguyên 31 bít cho phép ta tối ưu hóa nhiều // phần chính. Nhưng dù sao thì, hầu hết các trình duyệt hiện tại (tháng Tám năm 2014) không thể xử lý // các chuỗi 1 << 28 chars hoặc lớn hơn, vậy nên: if (str.length * count >= 1 << 28) { throw new RangeError('repeat count must not overflow maximum string size'); } var rpt = ''; for (var i = 0; i < count; i++) { rpt += str; } return rpt; } }
Thông số
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.repeat' in that specification. |
Standard | Định nghĩa bổ sung. |
ECMAScript Latest Draft (ECMA-262) The definition of 'String.prototype.repeat' in that specification. |
Draft |
Tương thích trình duyệt
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
repeat | Chrome Full support 41 | Edge Full support 12 | Firefox Full support 24 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android No support No | Chrome Android Full support 36 | Firefox Android Full support 24 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support 3.0 | nodejs
Full support
4.0.0
|
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.