Làm cách nào để bạn sử dụng nhãn trong javascript?

Mẹo. Thuộc tính for của

let x = 0;
// Label a loop as "myLoop"
myLoop:
while (true) {
  if (x >= 10) {
    // This will cause "myLoop" to end.
    break myLoop;
  }
  x++;
}
9 phải bằng thuộc tính id của phần tử liên quan để ràng buộc chúng với nhau. Nhãn cũng có thể được liên kết với một phần tử bằng cách đặt phần tử bên trong phần tử
let x = 0;
// Label a loop as "myLoop"
myLoop:
while (true) {
  if (x >= 10) {
    // This will cause "myLoop" to end.
    break myLoop;
  }
  x++;
}
9.  

Show

Nhãn là một tính năng đã tồn tại kể từ khi tạo JavaScript. Chúng không mới. Tôi không nghĩ nhiều người biết về chúng và thậm chí tôi còn cho rằng chúng hơi khó hiểu. Tuy nhiên, như chúng ta sẽ thấy, nhãn có thể hữu ích trong những trường hợp rất cụ thể

Nhưng trước tiên. Không nên nhầm lẫn nhãn JavaScript với mã HTML

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
1, đây là một điều hoàn toàn khác

Nhãn JavaScript là một cách để đặt tên cho câu lệnh hoặc khối mã. Tiêu biểu. vòng lặp và câu lệnh điều kiện. Điều đó cho phép bạn

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
2 hoặc
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
3 tuyên bố được dán nhãn từ bên trong. Để áp dụng nhãn cho một câu lệnh, hãy bắt đầu câu lệnh bằng
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
0 và bất kỳ thứ gì bạn đặt làm “nhãn” sẽ là nhãn mà bạn có thể tham khảo sau này

Đây là cú pháp cơ bản cho một nhãn

let x = 0;
// Label a loop as "myLoop"
myLoop:
while (true) {
  if (x >= 10) {
    // This will cause "myLoop" to end.
    break myLoop;
  }
  x++;
}

Nhãn chỉ là một tham chiếu nội bộ cho một câu lệnh và không phải là thứ có thể tra cứu, xuất hoặc lưu trữ trong một giá trị. Chúng cũng không xung đột với tên biến, vì vậy nếu bạn thực sự muốn gây nhầm lẫn cho mọi người, bạn có thể có một vòng lặp và một biến có cùng tên. Xin đừng làm điều này —  bạn trong tương lai và bất kỳ ai khác phải đọc mã của bạn sẽ đánh giá cao điều đó. Các trường hợp sử dụng cho nhãn bị hạn chế, nhưng cực kỳ mạnh mẽ trong tay phải

Giới thiệu ngắn gọn về const x = 1; switch(x) { case 1: console.log('On your mark!'); break; // Doesn't check the rest of the switch statement if 1 is true case 2: console.log('Get set!'); break; // Doesn't check the rest of the switch statement if 2 is true case 3: console.log('GO!'); break; // Doesn't check the rest of the switch statement if 3 is true } // logs: 'On your mark!'2 và const x = 1; switch(x) { case 1: console.log('On your mark!'); break; // Doesn't check the rest of the switch statement if 1 is true case 2: console.log('Get set!'); break; // Doesn't check the rest of the switch statement if 2 is true case 3: console.log('GO!'); break; // Doesn't check the rest of the switch statement if 3 is true } // logs: 'On your mark!'3

Hãy sao lưu một chút và nói về

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
2 và
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
3. Một câu lệnh
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
2 sẽ chấm dứt vòng lặp hiện đang chạy hoặc câu lệnh có điều kiện. Nó được sử dụng phổ biến nhất trong câu lệnh
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
6 để kết thúc câu lệnh
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
7, nhưng nó cũng có thể được sử dụng để kết thúc sớm câu lệnh
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
8 hoặc cũng để kết thúc vòng lặp
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
9 hoặc
for (let x = 0; x &< 10; x++) {
  if (x !== 5) continue; // If the number isn't five, go to the next pass of the loop.
  console.log(x);
}
// logs: 5
0 và dừng vòng lặp. Đó là một cách tuyệt vời để thoát ra khỏi câu lệnh điều kiện hoặc kết thúc vòng lặp sớm

Đây là một ví dụ cơ bản về

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
2 đang được sử dụng

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'

Tương tự, câu lệnh

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
3 có thể được sử dụng với các vòng lặp để kết thúc sớm vòng lặp hiện tại và bắt đầu lần chạy tiếp theo của vòng lặp. Tuy nhiên, điều này sẽ chỉ hoạt động bên trong các vòng lặp

Đây là

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
3 đang được sử dụng

for (let x = 0; x &< 10; x++) {
  if (x !== 5) continue; // If the number isn't five, go to the next pass of the loop.
  console.log(x);
}
// logs: 5

Sử dụng nhãn có const x = 1; switch(x) { case 1: console.log('On your mark!'); break; // Doesn't check the rest of the switch statement if 1 is true case 2: console.log('Get set!'); break; // Doesn't check the rest of the switch statement if 2 is true case 3: console.log('GO!'); break; // Doesn't check the rest of the switch statement if 3 is true } // logs: 'On your mark!'2

Thông thường, trường hợp sử dụng cho các nhãn xuất hiện khi bạn nhập các câu lệnh lồng nhau dưới bất kỳ hình thức nào. Sử dụng chúng với

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
2 có thể dừng một vòng lặp hoặc điều kiện được lồng sâu và khiến nó dừng ngay lập tức

Hãy đến với tiêu đề của bài đăng trên blog này

// Our outer if statement
outerIf: 
if (true) {
  // Our inner if statement
  innerIf:
  if (true) {
    break outerIf; // Immediately skips to the end of the outer if statement
  }
  console.log('This never logs!');
}

Bạn đã có nó rồi, bạn có thể dán nhãn cho câu lệnh

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
8

Sử dụng nhãn có const x = 1; switch(x) { case 1: console.log('On your mark!'); break; // Doesn't check the rest of the switch statement if 1 is true case 2: console.log('Get set!'); break; // Doesn't check the rest of the switch statement if 2 is true case 3: console.log('GO!'); break; // Doesn't check the rest of the switch statement if 3 is true } // logs: 'On your mark!'3

Đã có lúc tôi tạo một vòng lặp lồng nhau và muốn bỏ qua một số lần lặp của vòng lặp bên ngoài trong khi ở bên trong vòng lặp bên trong. Tôi thường kết thúc vòng lặp bên trong, sau đó kiểm tra xem mình có đang ở trạng thái muốn bỏ qua không, sau đó tiếp tục vòng lặp bên ngoài. Có thể đơn giản hóa mã đó thành một câu lệnh dễ đọc hơn là điều tuyệt vời

let x = 0;
// Label a loop as "myLoop"
myLoop:
while (true) {
  if (x >= 10) {
    // This will cause "myLoop" to end.
    break myLoop;
  }
  x++;
}
1

Chặn câu lệnh và nhãn

Các câu lệnh chặn trong JavaScript là một cách để đưa các biến

for (let x = 0; x &< 10; x++) {
  if (x !== 5) continue; // If the number isn't five, go to the next pass of the loop.
  console.log(x);
}
// logs: 5
8 và
for (let x = 0; x &< 10; x++) {
  if (x !== 5) continue; // If the number isn't five, go to the next pass of the loop.
  console.log(x);
}
// logs: 5
9 của bạn vào phạm vi chỉ một phần mã của bạn. Điều này có thể hữu ích nếu bạn muốn bản địa hóa một số biến mà không phải tạo hàm. Cảnh báo (lớn) cho điều này là các câu lệnh khối không hợp lệ ở chế độ nghiêm ngặt, đó là mô-đun ES theo mặc định

Đây là một câu lệnh khối được dán nhãn

let x = 0;
// Label a loop as "myLoop"
myLoop:
while (true) {
  if (x >= 10) {
    // This will cause "myLoop" to end.
    break myLoop;
  }
  x++;
}
4

sử dụng thế giới thực

Tôi đã mất một thời gian để tìm ra lý do sử dụng nhãn trong mã sản xuất hàng ngày. Điều này có thể hơi dài dòng, nhưng một điểm mà nhãn trong JavaScript có thể hữu ích là thoát sớm khỏi vòng lặp khi đang ở trong câu lệnh

const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
6. Vì bạn có thể
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
2 khi đang ở trong
const x = 1;
switch(x) {
  case 1:
    console.log('On your mark!');
    break; // Doesn't check the rest of the switch statement if 1 is true
  case 2:
    console.log('Get set!');
    break; // Doesn't check the rest of the switch statement if 2 is true
  case 3:
    console.log('GO!');
    break; // Doesn't check the rest of the switch statement if 3 is true
}
// logs: 'On your mark!'
6, nên việc có thể áp dụng nhãn cho một vòng lặp kết thúc sớm có khả năng làm cho mã của bạn hiệu quả hơn

Đây là cách chúng tôi có thể sử dụng nó trong một ứng dụng máy tính

let x = 0;
// Label a loop as "myLoop"
myLoop:
while (true) {
  if (x >= 10) {
    // This will cause "myLoop" to end.
    break myLoop;
  }
  x++;
}
7

Bằng cách này, chúng ta có thể thoát khỏi vòng lặp

// Our outer if statement
outerIf: 
if (true) {
  // Our inner if statement
  innerIf:
  if (true) {
    break outerIf; // Immediately skips to the end of the outer if statement
  }
  console.log('This never logs!');
}
3 khi một điều kiện được khớp thay vì cho phép tập lệnh tiếp tục

Phần kết luận

Rất hiếm khi bạn cần sử dụng nhãn JavaScript. Trên thực tế, bạn có thể có một sự nghiệp viên mãn mà không hề biết rằng điều này tồn tại. Tuy nhiên, nếu bạn thấy rằng một nơi mà cú pháp này hữu ích, thì giờ đây bạn được trao quyền để sử dụng nó

Làm cách nào để thêm nhãn mới trong JavaScript?

Giải pháp Tạo nhãn động trong Javascript sẽ được trình bày bằng các ví dụ trong bài viết này. .
var newlabel = tài liệu. createElement(“Nhãn”);
nhãn mới. setAttribute(“dành cho”, id_from_input);
nhãn mới. innerHTML = “Đây là văn bản”;
mẹDiv. appendChild(nhãn mới);

Làm cách nào để sử dụng câu lệnh nhãn trong Java?

Nhãn là một tên biến hợp lệ biểu thị tên của vòng lặp đến nơi điều khiển thực thi sẽ chuyển đến. Để gắn nhãn cho vòng lặp, hãy đặt nhãn trước vòng lặp với dấu hai chấm ở cuối. Do đó, vòng lặp có nhãn được gọi là vòng lặp có nhãn. .
tên nhãn
trong khi (. )
//các câu lệnh để thực hiện

Ví dụ về nhãn là gì?

Thuật ngữ 'nhãn' có thể chỉ một mảnh vải, giấy hoặc nhựa nhỏ được gắn vào sản phẩm. Nó có thông tin về sản phẩm đó. Ví dụ: các công ty may mặc gắn nhãn cho hàng may mặc . Nhãn có thông tin về chất liệu, kích cỡ và công ty sản xuất hàng may mặc.

Làm cách nào để kết thúc câu lệnh if trong JavaScript?

Câu lệnh ngắt sẽ chấm dứt vòng lặp hoặc câu lệnh điều kiện hiện đang chạy. Nó được sử dụng phổ biến nhất trong câu lệnh switch để kết thúc một trường hợp, nhưng nó cũng có thể được sử dụng để kết thúc sớm câu lệnh if hoặc cũng để khiến vòng lặp for hoặc while kết thúc và dừng vòng lặp.