[Lưu ý rằng một mảng có độ dài 11 chỉ nhận được 10 chữ "a", vì
var str = new Array[len + 1].join[ character ];
1 đặt đối số giữa các phần tử mảng. ]var str = new Array[len + 1].join[ character ];
for [var e = ''; e.length < n;]
e += c;
return e;
readyreturn Array[n].join[c];
http. //jsperf. com/ký tự lặp lại
Bạn đã thử với một vòng lặp chưa
for [var i = 0; i < 3; i++] {
s += " "; }
Đây là một cách gọn gàng không có vòng lặp. Ngoài việc ngắn gọn, tôi khá chắc chắn rằng việc sử dụng phép nối sẽ hiệu quả hơn nhiều đối với các chuỗi rất lớn
Phương thức repeat[]
xây dựng và trả về một chuỗi mới chứa số bản sao được chỉ định của chuỗi mà nó được gọi, nối với nhau
repeat[count]
count
Một số nguyên giữa 0
và +Infinity
, cho biết số lần lặp lại chuỗi
Một chuỗi mới chứa số bản sao được chỉ định của chuỗi đã cho
RangeError
. số lần lặp lại phải không âmRangeError
. số lần lặp lại phải nhỏ hơn vô cùng và không vượt quá kích thước chuỗi tối đa
"abc".repeat[-1]; // RangeError
"abc".repeat[0]; // ''
"abc".repeat[1]; // 'abc'
"abc".repeat[2]; // 'abcabc'
"abc".repeat[3.5]; // 'abcabcabc' [count will be converted to integer]
'abc'.repeat[1 / 0]; // RangeError
[{ toString: [] => 'abc', repeat: String.prototype.repeat }].repeat[2];
// 'abcabc' [repeat[] is a generic method]
Bảng BCD chỉ tải trong trình duyệt có bật JavaScript. Bật JavaScript để xem dữ liệu
0"abc".repeat[-1]; // RangeError "abc".repeat[0]; // '' "abc".repeat[1]; // 'abc' "abc".repeat[2]; // 'abcabc' "abc".repeat[3.5]; // 'abcabcabc' [count will be converted to integer] 'abc'.repeat[1 / 0]; // RangeError [{ toString: [] => 'abc', repeat: String.prototype.repeat }].repeat[2]; // 'abcabc' [repeat[] is a generic method]
Tìm thấy một vấn đề nội dung với trang này?
- Chỉnh sửa trang trên GitHub
- Báo cáo vấn đề nội dung
- Xem nguồn trên GitHub
Trang này được sửa đổi lần cuối vào ngày 13 tháng 12 năm 2022 bởi những người đóng góp MDN
Vòng lặp cung cấp một cách nhanh chóng và dễ dàng để làm điều gì đó lặp đi lặp lại. Chương này của Hướng dẫn JavaScript giới thiệu các câu lệnh lặp khác nhau có sẵn cho JavaScript
Bạn có thể coi vòng lặp là một phiên bản vi tính hóa của trò chơi trong đó bạn yêu cầu ai đó thực hiện X bước theo một hướng, sau đó Y bước theo hướng khác. Ví dụ: ý tưởng "Đi năm bước về phía đông" có thể được diễn đạt theo cách này dưới dạng một vòng lặp
________số 8_______
Có nhiều loại vòng lặp khác nhau, nhưng về cơ bản chúng đều làm cùng một việc. họ lặp lại một hành động một số lần. [Lưu ý rằng có thể số đó có thể bằng không. ]
Các cơ chế vòng lặp khác nhau cung cấp các cách khác nhau để xác định điểm bắt đầu và điểm kết thúc của vòng lặp. Có nhiều tình huống dễ dàng được phục vụ bởi một loại vòng lặp hơn các loại vòng lặp khác
Các câu lệnh cho các vòng lặp được cung cấp trong JavaScript là
Vòng lặp
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 lặp lại cho đến khi một điều kiện cụ thể đánh giá là sai. Vòng lặp JavaScript
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 tương tự như vòng lặp Java và C
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3Một câu lệnh
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 trông như saufor [initialization; condition; afterthought]
statement
Khi một vòng lặp
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 thực thi, điều sau xảy ra- Biểu thức khởi tạo
8, nếu có, được thực thi. Biểu thức này thường khởi tạo một hoặc nhiều bộ đếm vòng lặp, nhưng cú pháp cho phép biểu thức ở bất kỳ mức độ phức tạp nào. Biểu thức này cũng có thể khai báo biếnChoose some music types, then click the button below: R&B Jazz Blues New Age Classical Opera How many are selected?
- Biểu thức
9 được đánh giá. Nếu giá trị củaChoose some music types, then click the button below: R&B Jazz Blues New Age Classical Opera How many are selected?
9 là true, các câu lệnh lặp sẽ thực thi. Ngược lại, vòng lặpChoose some music types, then click the button below: R&B Jazz Blues New Age Classical Opera How many are selected?
3 kết thúc. [Nếu biểu thứcChoose some music types, then click the button below: R&B Jazz Blues New Age Classical Opera How many are selected?
9 bị bỏ qua hoàn toàn, điều kiện được coi là đúng. ]Choose some music types, then click the button below: R&B Jazz Blues New Age Classical Opera How many are selected?
3 thực hiện. Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối [function countSelected[selectObject] { let numberSelected = 0; for [let i = 0; i { const musicTypes = document.selectForm.musicTypes; console.log[`You have selected ${countSelected[musicTypes]} option[s].`]; }];
4] để nhóm các câu lệnh đófunction countSelected[selectObject] { let numberSelected = 0; for [let i = 0; i { const musicTypes = document.selectForm.musicTypes; console.log[`You have selected ${countSelected[musicTypes]} option[s].`]; }];
- Nếu có, biểu thức cập nhật
5 được thực thifunction countSelected[selectObject] { let numberSelected = 0; for [let i = 0; i { const musicTypes = document.selectForm.musicTypes; console.log[`You have selected ${countSelected[musicTypes]} option[s].`]; }];
- Điều khiển quay lại Bước 2
Trong ví dụ bên dưới, hàm chứa câu lệnh
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 đếm số tùy chọn đã chọn trong danh sách cuộn [phần tử function countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
7 cho phép nhiều lựa chọn]HTML
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
JavaScript
Ở đây, câu lệnh
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 khai báo biến function countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
9 và khởi tạo nó thành do
statement
while [condition];
0. Nó kiểm tra xem function countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
9 có ít hơn số tùy chọn trong phần tử function countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
7 hay không, thực hiện câu lệnh do
statement
while [condition];
3 tiếp theo và tăng function countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
9 lên 1 sau mỗi lần đi qua vòng lặpfunction countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
Câu lệnh
do
statement
while [condition];
5 lặp lại cho đến khi một điều kiện cụ thể được đánh giá là saiMột tuyên bố
do
statement
while [condition];
5 trông như saudo
statement
while [condition];
function countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
3 luôn được thực hiện một lần trước khi điều kiện được kiểm tra. [Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối [function countSelected[selectObject] {
let numberSelected = 0;
for [let i = 0; i {
const musicTypes = document.selectForm.musicTypes;
console.log[`You have selected ${countSelected[musicTypes]} option[s].`];
}];
4] để nhóm các câu lệnh đó. ]Nếu
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
9 là let i = 0;
do {
i += 1;
console.log[i];
} while [i
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
9 trở thành let i = 0;
do {
i += 1;
console.log[i];
} while [i
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
được kiểm tra lại. Nếu điều kiện trả về let i = 0;
do {
i += 1;
console.log[i];
} while [i Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 và tiếp tục thực hiện vòng lặp với lần lặp tiếp theo. Ngược lại với câu lệnh label:
statement
4, label:
statement
5 không chấm dứt hoàn toàn việc thực hiện vòng lặp. Trong một vòng lặp let i = 0;
do {
i += 1;
console.log[i];
} while [i
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3, nó nhảy tới vòng lặp for [initialization; condition; afterthought]
statement
24label:
statement
5 với nhãn, nó sẽ áp dụng cho câu lệnh lặp được xác định bằng nhãn đóCú pháp của câu lệnh
label:
statement
5 như saufor [initialization; condition; afterthought]
statement
4Ví dụ sau đây cho thấy một vòng lặp
let i = 0;
do {
i += 1;
console.log[i];
} while [i
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
3 truyền thống với chỉ mục số khi lặp qua các mảng, bởi vì câu lệnh for [initialization; condition; afterthought]
statement
54 lặp qua các thuộc tính do người dùng xác định ngoài các phần tử mảng, nếu bạn sửa đổi đối tượng Array [chẳng hạn như thêm các thuộc tính tùy chỉnh hoặc Câu lệnh
for [initialization; condition; afterthought]
statement
64 tạo một vòng lặp Lặp lại trên các đối tượng có thể lặp lại [bao gồm đối tượng for [initialization; condition; afterthought]
statement
60, for [initialization; condition; afterthought]
statement
66, for [initialization; condition; afterthought]
statement
67, for [initialization; condition; afterthought]
statement
68, v.v.], gọi một hook lặp tùy chỉnh với các câu lệnh sẽ được thực thi cho giá trị của từng thuộc tính riêng biệt
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
0Ví dụ sau đây cho thấy sự khác biệt giữa vòng lặp
for [initialization; condition; afterthought]
statement
64 và vòng lặp for [initialization; condition; afterthought]
statement
54. Trong khi for [initialization; condition; afterthought]
statement
54 lặp lại tên thuộc tính, thì for [initialization; condition; afterthought]
statement
64 lặp lại giá trị thuộc tính
Choose some music types, then click the button below:
R&B
Jazz
Blues
New Age
Classical
Opera
How many are selected?
1Các câu lệnh
for [initialization; condition; afterthought]
statement
64 và for [initialization; condition; afterthought]
statement
54 cũng có thể được sử dụng với hàm hủy. Ví dụ: bạn có thể lặp đồng thời các khóa và giá trị của một đối tượng bằng cách sử dụng for [initialization; condition; afterthought]
statement
75