Cho biết các phát biểu sau đúng hay sai
[a] JavaScript là ngôn ngữ kịch bản phía máy khách duy nhất
[b] JavaScript là ngôn ngữ kịch bản phân biệt chữ hoa chữ thường
[c] Từ khóa dùng để khai báo một biến trong JavaScript là VAR
Kể từ khi được phát hành vào ngày 4 tháng 12 năm 1995 [sáu tháng sau Java, được phát hành vào ngày 23 tháng 5 năm 1995], JavaScript đã trải qua nhiều thay đổi. JavaScript bắt đầu là ngôn ngữ lập trình phía máy khách [chạy bên trong trình duyệt web thông qua công cụ JavaScript tích hợp] để thêm nội dung tương tác vào trang web. Nó trở nên mạnh mẽ hơn với DHTML [1997] và Ajax [1999]. với nút. js [phát hành vào ngày 27 tháng 5 năm 2009], JavaScript có thể được sử dụng để lập trình phía máy chủ và xây dựng các ứng dụng web đầy đủ, cũng như các tập lệnh tiện ích độc lập. Vào năm 2015, ECMAScript 6 [ES6 hoặc ES2015] giới thiệu bản cập nhật lớn cho ngôn ngữ, chẳng hạn như các lớp và mô-đun, đồng thời biến JavaScript thành ngôn ngữ lập trình đa năng có khả năng thực hiện các tác vụ phần mềm phức tạp
Giới thiệu
Nó bắt đầu dưới dạng Ngôn ngữ lập trình phía máy khách chạy bên trong trình duyệt web thông qua Công cụ JavaScript tích hợpJavaScript là ngôn ngữ lập trình phía máy khách được sử dụng rộng rãi nhất cho phép bạn nâng cấp HTML của mình bằng tính tương tác, hoạt ảnh và hiệu ứng hình ảnh động để có Giao diện người dùng và Trải nghiệm người dùng [UI/UX] tốt hơn. Nó là
- một ngôn ngữ kịch bản nhỏ, nhẹ, hướng đối tượng, đa nền tảng, có mục đích đặc biệt được chạy trong môi trường máy chủ [thường là trình duyệt web]
- ngôn ngữ kịch bản phía máy khách để làm phong phú thêm giao diện người dùng web và tạo các trang web động [e. g. , vì. xác thực đầu vào và phản hồi ngay lập tức cho hành động của người dùng]
- công cụ hỗ trợ AJAX [JavaScript và XML không đồng bộ - có thể được sử dụng để cập nhật một phần của trang web một cách không đồng bộ], tạo ra sự quan tâm mới đối với JavaScript
JavaScript hoạt động cùng với HTML/CSS. HTML cung cấp nội dung; . Cùng nhau, chúng làm phong phú UI/UX của người dùng web
JavaScript hiện có ở mọi nơi với Node. jsJavaScript đã phát triển vượt ra ngoài lập trình phía máy khách. Với sự ra đời của Node. js vào năm 2009 [môi trường thời gian chạy JavaScript đa nền tảng, mã nguồn mở], bạn có thể chạy JavaScript độc lập hoặc bên trong máy chủ [thay vì trình duyệt]. Điều này cho phép bạn sử dụng một ngôn ngữ duy nhất cho cả lập trình phía máy chủ và phía máy khách
Lịch sử và Phiên bảnJavaScript, ban đầu được gọi là LiveScript, được tạo bởi Brendan Eich tại Netscape vào năm 1995. Ngay sau đó, Microsoft đã tung ra phiên bản JavaScript của riêng mình có tên là JScript. Sau đó, Netscape gửi nó lên ECMA [trước đây là "Hiệp hội các nhà sản xuất máy tính châu Âu", nay là "Ecma International - hiệp hội tiêu chuẩn hóa hệ thống thông tin và truyền thông châu Âu"] để chuẩn hóa, cùng với JScript của Microsoft.
Đặc tả ECMA được gọi là "Đặc tả ngôn ngữ ECMA-262 ECMAScript" @ http. //www. ecma-quốc tế. org/ấn phẩm/tiêu chuẩn/Ecma-262. htm [cũng được phê duyệt là "ISO/IEC 16262"]
- ECMA-262 phiên bản 1 [tháng 6 năm 1997]. Ấn bản đầu tiên
- ECMA-262 phiên bản 2 [tháng 8 năm 1998]
- ECMA-262 phiên bản 3 [12/1999]. Đã thêm biểu thức chính quy, thử/bắt, chuyển đổi, do-while, v.v.
- ECMA-262 phiên bản 4 - Bị bỏ rơi do sự khác biệt về chính trị. Vào năm 2007, TC-39 [ủy ban chịu trách nhiệm về ECMAScript] đã đưa ra một đặc điểm kỹ thuật dự thảo cho ECMAScript 4, có quy mô lớn và giới thiệu nhiều cú pháp và tính năng mới. Nhưng một nhóm các nhà phát triển từ Yahoo, Google và Microsoft cảm thấy như vậy là quá nhiều và đã tạo ra một đề xuất thay thế có tên là ECMAScript 3. 1. ECMAScript 4 chưa bao giờ được hoàn thiện
- ECMA-262 phiên bản 5 và 5. 1 [tháng 6 năm 2011]. ECMAScript 3. 1 cuối cùng đã được chuẩn hóa thành ECMAScript 5. Đã thêm "chế độ nghiêm ngặt", phương thức lặp JSON, Array, v.v.
- ECMA-262 2015. phổ biến nhất được gọi là ECMAScript 6 hoặc ES6
Phiên bản này đã thêm cú pháp mới quan trọng để viết ứng dụng phức tạp, bao gồm khai báo lớp,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7 cho khai báo cục bộ,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 cho khai báo hằng cục bộ, giá trị tham số mặc định, bộ lặp và cho. của các vòng lặp, trình tạo kiểu Python, biểu thức hàm mũi tên [_______0_______9], dữ liệu nhị phân, mảng đã nhập, bộ sưu tập mới [bản đồ, bộ và Bản đồ yếu], lời hứa, sự phản chiếu, proxy, chữ mẫu cho chuỗi, v.v. - ECMAScript 2016 [ES7]. Kể từ ES6, các tiêu chuẩn ECMAScript sẽ có chu kỳ phát hành hàng năm vào tháng 6
Phiên bản này đã thêm toán tử hàm mũ [
0], phạm vi khối của các biến và hàm, từ khóaJavaScript Example: Loop
Testing Loop
1 vàJavaScript Example: Loop
Testing Loop
2 cho lập trình không đồng bộ, v.v.JavaScript Example: Loop
Testing Loop
- ECMAScript 2017 [ES8]. Đã thêm phần đệm chuỗi, các hàm không đồng bộ sử dụng trình tạo và lời hứa,
3,JavaScript Example: Loop
Testing Loop
4 để thao tác đối tượng dễ dàng, v.v.JavaScript Example: Loop
Testing Loop
- ECMAScript 2018 [ES9]. Đã thêm tham số phần còn lại [
5], toán tử trải rộng, phép lặp không đồng bộ, bổ sung cho biểu thức chính quyJavaScript Example: Loop
Testing Loop
- ECMAScript 2019 [ES10]. Đã thêm
6,JavaScript Example: Loop
Testing Loop
7, thay đổiJavaScript Example: Loop
Testing Loop
8 vàJavaScript Example: Loop
Testing Loop
9, ràng buộc bắt trở thành tùy chọn, v.v.JavaScript Example: Loop
Testing Loop
- ECMAScript 2020 [ES11]. giới thiệu một kiểu nguyên thủy
for [ initialization ; test ; post-processing ] { body ; }
0 cho các số nguyên có kích thước tùy ý, toán tử kết hợp nullish [for [ initialization ; test ; post-processing ] { body ; }
1] và đối tượngfor [ initialization ; test ; post-processing ] { body ; }
2 - ECMAScript 2021 [ES12]. Đã thêm tính năng nâng cao cho chuỗi [
for [ initialization ; test ; post-processing ] { body ; }
3], lời hứa [for [ initialization ; test ; post-processing ] { body ; }
4] và tham chiếu đối tượng. Toán tử gán logic [for [ initialization ; test ; post-processing ] { body ; }
5,for [ initialization ; test ; post-processing ] { body ; }
6,for [ initialization ; test ; post-processing ] { body ; }
7,]
Java là một ngôn ngữ lập trình đa năng chính thức. Nó được tạo ra bởi James Gosling tại Sun Microsystems [nay là một phần của Oracle] và phát hành vào tháng 8 năm 1995
JavaScript được tạo bởi Brendan Eich tại Netscape, cũng vào năm 1995. Ban đầu được gọi là LiveScript, nó là một ngôn ngữ chuyên dụng nhỏ và nhẹ để viết chương trình phía máy khách chạy bên trong trình duyệt để tạo giao diện người dùng hoạt động và tạo các trang web động. Nó được đổi tên thành JavaScript trong một quyết định tiếp thị tồi tệ nhằm cố gắng tận dụng sự phổ biến của ngôn ngữ Java, khi Netscape phát hành Navigator 2 vào năm 1996
Java và JavaScript là các ngôn ngữ hoàn toàn khác nhau cho các mục đích lập trình khác nhau. Tuy nhiên, trong những ngày đầu, một số nỗ lực đã được thực hiện để áp dụng cú pháp và quy ước của Java vào JavaScript, vì vậy JavaScript dường như là một tập hợp con của Java. Trên thực tế, chúng có rất ít điểm chung
JavaScript phía máy khách KHÔNG THỂ làm gì?Hãy nhớ rằng JavaScript là chương trình phía máy khách mà bạn đã tải xuống từ máy chủ và chạy bên trong trình duyệt của máy [máy khách] của bạn. Làm gì để ngăn ai đó viết JavaScript xóa sạch đĩa cứng của bạn hoặc kích hoạt một cuộc tấn công từ chối dịch vụ tới một máy chủ khác?
- Nó không thể đọc tệp từ máy của khách hàng
- Nó chỉ có thể kết nối với máy chủ mà nó đến từ đó. Nó có thể đọc tệp từ máy chủ mà nó đến từ. Nó không thể ghi tập tin vào máy chủ
- Nó không thể kết nối với máy chủ khác
- Nó không thể đóng một cửa sổ mà nó không mở
jQuery là một thư viện JavaScript đa trình duyệt phổ biến. jQuery là JavaScript [nhưng có cú pháp mở rộng riêng], do đó, bạn cần hiểu JavaScript. Tôi khuyên bạn nên đọc qua cú pháp JavaScript [và đặc biệt chú ý đến các đối tượng], sau đó chuyển sang jQuery để sản xuất. Đọc "Thông tin cơ bản về jQuery"
JavaScript độc lập và phía máy chủ với nút. Công cụ JavaScript js[LÀM]
Trình chỉnh sửa mã nguồn JavaScript và IDEBạn cần một trình soạn thảo văn bản để viết JavaScript của mình. Bạn có thể sử dụng trình soạn thảo văn bản thuần túy như NotePad của Windows hoặc TextEdit của macOS [KHÔNG được khuyến nghị nghiêm ngặt vì chúng không thể tô sáng cú pháp]. Để cải thiện năng suất của bạn, một trình soạn thảo mã nguồn tốt [cung cấp tính năng đánh dấu cú pháp, tự động hoàn thành mã, đoạn mã, tài liệu, điều hướng ký hiệu, tái cấu trúc, v.v. ] là điều cần thiết. Có rất nhiều phần mềm miễn phí/phần mềm chia sẻ, chẳng hạn như Visual Studio Code [VS Code], Sublime Text, Atom, Brackets, Komodo Edit, Emacs, Vim, BBEdit, TextMate, NotePad++ [Windows], v.v. Bạn cũng có thể sử dụng IDE quy mô đầy đủ như Eclipse, NetBeans, IntelliJ IDEA. Ngoài ra còn có nhiều trình soạn thảo và trình biên dịch "trực tuyến" [e. g. , Programiz, JSFiddle, Playcode], nhưng tôi nghĩ rằng bạn nên cài đặt của riêng mình
Ví dụ JavaScript phía máy khách
Tôi sẽ cho rằng bạn biết HTML và CSS [hãy đọc các bài viết về HTML/CSS của tôi nếu không]. Tôi cũng sẽ giả định rằng bạn hiểu một số điều cơ bản về lập trình [tư duy tính toán] chẳng hạn như biến, cấu trúc if-else và vòng lặp for
JavaScript phía máy khách chạy bên trong trình duyệt thông qua công cụ JavaScript tích hợp. Có các tiêu chuẩn về JavaScript. Nhưng Big-5 [Chrome, Firefox, IE/Edge, Safari và Opera], đặc biệt là IE, không tuân thủ nghiêm ngặt tất cả các tiêu chuẩn. Hơn nữa, họ tạo tiện ích mở rộng của riêng mình. Do đó, hành vi của JavaScript có thể khác nhau trong các trình duyệt khác nhau. Bạn có thể cần kiểm tra JavaScript của mình trên nhiều trình duyệt
JavaScript cũng chạy độc lập [và trong máy chủ]. Để chạy JavaScript độc lập, bạn cần cài đặt Node. js, một công cụ JavaScript. Tôi sẽ trình bày các ví dụ trong phần tiếp theo
JS phía máy khách EG 1. Hàm for [ initialization ; test ; post-processing ] {
body ;
}
8 và for [ initialization ; test ; post-processing ] {
body ;
}
9
Hãy để chúng tôi viết JavaScript phía máy khách đầu tiên của chúng tôi để in thông báo "Xin chào, thế giới"
Bắt đầu với một tệp mới và nhập các mã sau. Không nhập số dòng, được sử dụng để hỗ trợ giải thích. lưu ý rằng
- JavaScript phân biệt chữ hoa chữ thường. Một
0 KHÔNG phải là mộtJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
1 và KHÔNG phải là mộtJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
2JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- Khoảng trắng "thêm" [khoảng trống, tab và dòng mới] bị bỏ qua. Nghĩa là, nhiều khoảng trắng được coi là một ký tự trống. Bạn có thể sử dụng chúng một cách tự do để làm cho chương trình của bạn dễ đọc hơn
Lưu tệp dưới dạng "
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
3" [hoặc bất kỳ tên tệp nào bạn thích, với phần mở rộng tệp là "
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
4" hoặc "
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
5"]. Chạy tập lệnh bằng cách tải tệp HTML vào trình duyệt hỗ trợ JavaScript [e. g. , Một trong BIG FIVE - Chrome, Firefox, Internet Explorer/Edge, Safari hoặc Opera]
JavaScript Example: Functions alert[] and document.write[]
My first JavaScript says:
Làm thế nào nó hoạt động?- JavaScript là mã lập trình được nhúng bên trong tài liệu HTML. Các mã được chứa giữa một cặp thẻ
6, như sau. GHI CHÚ. Trong HTML4/XHTML, bạn cần thêm thuộc tínhJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
7 vào phần tửJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
6. Dòng 7-9 và Dòng 13-17 là hai đoạn mã JavaScript, được đặt lần lượt trong phầnJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
9 vàJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
0JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- Các câu lệnh JavaScript được kết thúc bằng dấu chấm phẩy
1 [như Java/C/C++/C#]JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- Hàm
2 [Dòng 8] bật ra một hộp thoại hiển thịJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
3 và một nút OK. Các chuỗi được đặt trong cặp dấu nháy kép hoặc nháy đơn, e. g. ,JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
4 hoặcJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
5JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- Trang web hiện tại được đại diện bởi cái gọi là đối tượng
6 trong JavaScript. Thuộc tínhJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
7 [Dòng 16] lưu trữ ngày sửa đổi cuối cùng của tài liệu hiện tại. HàmJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
8 [Dòng 14 đến 16] có thể được sử dụng để ghiJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
3 đã chỉ định vào tài liệu hiện tại, như một phần của tài liệu HTML hiện tạiJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- Toán tử
My First JavaScript says
00 [Dòng 16] có thể được sử dụng để nối hai chuỗi [như Java]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Theo kết quả của
for [ initialization ; test ; post-processing ] { body ; }
9, phần
0 của tài liệu này chứa.JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
2 vàJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
8 là một số hàm tích hợp thường được sử dụng được cung cấp trong JavaScriptJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
THỬ. In tiêu đề của tài liệu và vị trí URL. [Gợi ý. sử dụng thuộc tính
My First JavaScript says05 vàHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says06. ]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Không bị bộ nhớ cache bắt - Thực hiện Control-Refresh [hoặc Control-F5]
Nếu bạn sửa đổi mã và tải lại trang web, mã mới có thể không được thực thi vì trình duyệt lưu phiên bản đã tải trước đó vào bộ đệm. Bạn có thể sử dụng Control-F5 [Control-Refresh] để yêu cầu trình duyệt loại bỏ bộ đệm và tìm nạp một trang mới
Không sử dụng
for [ initialization ; test ; post-processing ] { body ; }8
Bởi vì không ai thích nó
"Việc sử dụng cảnh báo thường không được khuyến khích để ủng hộ các phương pháp khác không chặn người dùng tương tác với trang - nhằm tạo trải nghiệm người dùng tốt hơn. Tuy nhiên, nó có thể hữu ích để gỡ lỗi. "
for [ initialization ; test ; post-processing ] { body ; }8 về mặt kỹ thuật là tài sản của đối tượng
My First JavaScript says09 và sẽ được gọi làHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says10. Nhưng vìHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says09 là một đối tượng toàn cục nên tất cả các biến của nó đều là biến toàn cục và có thể được tham chiếu mà không cần tên đối tượngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Công cụ gỡ lỗi và công cụ phát triển web F12 của trình duyệt
Trước khi chúng tôi chuyển sang ví dụ tiếp theo, bạn nên dùng thử Công cụ phát triển web, có sẵn trong hầu hết các trình duyệt để gỡ lỗi HTML/CSS/JavaScript/ứng dụng web
- Khởi chạy trình duyệt của bạn và nhấn F12 để kích hoạt Công cụ phát triển web [Tôi đã thử trên Firefox, Chrome, Edge]
- Chọn ngăn "Bảng điều khiển". Bạn có thể nhập các câu lệnh JavaScript trong bảng điều khiển. Ví dụ: nhập từng câu lệnh JavaScript sau và quan sát kết quả.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
ghi chú.
My First JavaScript says12 ghi thông điệp vào bảng điều khiển, thường được sử dụng để gỡ lỗi [nhưHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says13 trong Java hoặcHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says14 trong C]Các thông báo lỗi ở đâu?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Nếu bạn mắc bất kỳ lỗi cú pháp nào trong ví dụ trước, bạn sẽ không thấy bất kỳ thông báo lỗi nào xuất hiện trên trình duyệt mà chỉ đơn giản là tập lệnh không chạy. Không thể gỡ lỗi chương trình mà không có thông báo lỗi. Rất may, bạn thực sự có thể tìm thấy các thông báo lỗi trong bảng điều khiển. Thực hiện một số lỗi và làm mới trình duyệt
JS phía máy khách EG 2. Biến và Hàm My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
15, My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
16
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Tập lệnh này nhắc người dùng nhập tên của họ, xác nhận tên và in thông báo chào mừng
Có ba loại hộp thoại bật lên để tương tác với người dùng
- Hàm
2 đặtJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
3 trên hộp bật lên có nút OK. Người dùng cần nhấp vào nút OK để tiếp tụcJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
- Hàm
My First JavaScript says
19 hiển thị hộp bật lên đầu vào vớiHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
20 với các nút OK và Hủy. Nó trả về đầu vào do người dùng nhập dưới dạng một chuỗi; . Tham số tùy chọnHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
22 chỉ định chuỗi ban đầu sẽ được hiển thị. Trong bài viết này mình sẽ chỉ ra các tham số chức năng tùy chọn có đuôiHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
23 cho gọnHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Hàm
My First JavaScript says
24 đặtHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
25 trên hộp bật lên có các nút OK và Hủy. Nó trả vềHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
26 nếu người dùng nhấn nút OK;Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Làm thế nào nó hoạt động?- Dòng 8 khai báo một biến toàn cục có tên là
My First JavaScript says
28, thông qua từ khóaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
29. Một biến là một vị trí lưu trữ được đặt tên chứa một giá trị. Sau khi khai báo biến, bạn có thể gán [và gán lại] giá trị cho biến đó, thông qua toán tử gánHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
30 [Dòng 9]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Dòng 9 gọi hàm
My First JavaScript says
19 để bật ra một hộp thoại và đọc chuỗi do người dùng nhập vào. Chuỗi đọc được gán cho biếnHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
28. HàmHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
15 tương tự như hàmHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
for [ initialization ; test ; post-processing ] { body ; }
8, nhưng nó chấp nhận đầu vào của người dùng - Ở Dòng 10, hàm
My First JavaScript says
24 hiển thị thông báo và trả vềHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
26 hoặcHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
27, tùy thuộc vào việc người dùng nhấn nút OK hay CancelHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Nếu kết quả là
My First JavaScript says
26, Dòng 11 in raHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
39. Nếu không, Dòng 13 in ra _______44_______40Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
THỬ. Thay vì in tin nhắn chúc mừng bằng cách sử dụng
for [ initialization ; test ; post-processing ] { body ; }9, hãy thực hiện điều đó thông qua một
for [ initialization ; test ; post-processing ] { body ; }8
JS phía máy khách EG 3. Đối tượng My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
43
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Tập lệnh sau tạo một đối tượng
My First JavaScript says43 biểu thị ngày giờ hiện tại và in thời gian hiện tạiHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: The Date object
Làm thế nào nó hoạt động?- Dòng 8 khai báo một biến có tên là
My First JavaScript says
45. Nó cũng tạo một đối tượngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
43 [thông qua toán tửHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
47], chứa dấu ngày-giờ hiện tại và gán nó choHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
45Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- "______44_______49" bắt đầu chú thích cuối dòng [Dòng 8 và 9]. Các nhận xét bị công cụ JavaScript bỏ qua nhưng quan trọng trong việc giải thích mã của bạn cho người khác [và cho chính bạn ba ngày sau]
- Dòng 9 khai báo một biến có tên là
My First JavaScript says
50. Nó cũng gọi hàmHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
51 trên đối tượngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
45, ở dạngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
53, để truy xuất phần giờ của đối tượngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
45 và gán nó cho biếnHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
50. Dòng 9 và 10, tương tự, xử lý phút và giâyHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Dòng 12 đến 14 sử dụng
My First JavaScript says
56 để ghi vào tài liệu hiện tại.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
57 [write-line] viết một dòng mới [Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
58] sau chuỗi đã cho. Hãy lưu ý rằng trình duyệt bỏ qua khoảng trắng thừa [dòng mới, khoảng trống, tab] trong tài liệu HTML;Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Dòng 16-20 chứa câu lệnh if-then-else có điều kiện. Tùy thuộc vào giá trị của
My First JavaScript says
50, một trong các thông báo sẽ được hiển thịHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Sửa đổi tập lệnh trên để in ngày, tháng, năm và ngày hiện tại trong tuần. [Gợi ý. Sử dụng các hàm
My First JavaScript says
62,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
63,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
64 vàHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
65 của đối tượngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
43.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
62 trả về 1-31.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
63 trả về 0 đến 11 cho tháng 1 đến tháng 12.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
64 trả về năm có 4 chữ số.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
65 trả về 0 đến 6 cho Chủ Nhật đến Thứ Bảy]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Sử dụng câu lệnh điều kiện để in ngày trong tuần trong word [i. e. , 0 cho Chủ Nhật, 1 cho Thứ Hai, v.v. ]. [Gợi ý. Sử dụng cấu trúc
My First JavaScript says
71 như sau. ]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
JS phía máy khách EG 4. vòng lặp
Tập lệnh sau nhắc người dùng về hệ số nhân và in bội số từ 1 đến 100 bằng vòng lặp for
JavaScript Example: Loop
Testing Loop
Làm thế nào nó hoạt động?- Dòng 11 nhắc người dùng nhập một số và gán nó cho biến
My First JavaScript says
72Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Dòng 12-14 chứa vòng lặp for. Một vòng lặp for có cú pháp sau.
for [ initialization ; test ; post-processing ] { body ; }
Có bốn phần trong vòng lặp for. Ba trong số đó, khởi tạo, kiểm tra và xử lý hậu kỳ, được đặt trong ngoặc _______44_______73 và được phân tách bằng 2 dấu chấm phẩy. Phần thân chứa nhiệm vụ lặp đi lặp lại sẽ được thực hiện. Câu lệnh khởi tạo được thực thi đầu tiên. Bài kiểm tra sau đó được đánh giá. Nếu kiểm tra trả về true, phần thân được thực thi; . Thử nghiệm được đánh giá lại và quá trình lặp lại cho đến khi thử nghiệm trả về sai. Khi kiểm tra là sai, vòng lặp for hoàn thành và việc thực thi chương trình tiếp tục đến câu lệnh tiếp theo sau vòng lặp for. Lưu đồ sau đây minh họa quy trình vòng lặp for.
Trong ví dụ này, biến
My First JavaScript says
74 được khởi tạo thành 1. NếuHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
74 nhỏ hơn hoặc bằng 100, phần thân của vòng lặp sẽ thực thi, tiếp theo là câu lệnh xử lý hậu kỳ, làm tăng giá trị củaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
74 lên 1. Vòng lặp lặp lại cho đến khi giá trị củaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
74 KHÔNG nhỏ hơn hoặc bằng 100 [i. e. , nhiều hơn 100]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Sửa đổi tập lệnh trên để nhắc người dùng về số nhân cũng như số bội số được in [trong hai câu lệnh
My First JavaScript says
15]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Sửa đổi tập lệnh trên để chỉ in các bội số là số lẻ. [Gợi ý. Toán tử modulo "
My First JavaScript says
79" có thể được sử dụng để tính toán phần còn lại, e. g. ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
80 tính phần còn lại củaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
81 chia cho 2, kết quả là 0 hoặc 1. ]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JS phía máy khách EG 5. Hàm do người dùng định nghĩa và Trình xử lý sự kiện My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
82
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Bên cạnh các hàm có sẵn trong JavaScript như
for [ initialization ; test ; post-processing ] { body ; }8,
My First JavaScript says15,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says85 vàHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says57, bạn có thể xác định các hàm của riêng mình. Một hàm có tên và phần thân bao gồm một tập hợp các câu lệnh JavaScript cùng thực hiện một nhiệm vụ nhất định. Nó có thể lấy 0 hoặc nhiều đối số từ người gọi và trả lại 0 hoặc một giá trị cho người gọiHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Làm thế nào nó hoạt động?- Các dòng 8-10 định nghĩa một hàm có tên là
My First JavaScript says
87, thông qua từ khóaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
88. Hàm này gọi hàm tích hợp sẵnHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
89, hàm này sẽ mở một cửa sổ [hoặc tab] trình duyệt mới và tải trang "Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
90"Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Dòng 15-16 tạo nút HTML. Nhấp vào nút sẽ kích hoạt trình xử lý sự kiện
My First JavaScript says
82, tôi. e. ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
87 được xác định trước đóHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
THỬ. Bao gồm một nút khác, mở ra "______44_______93"
JS phía máy khách EG 6. Thêm Trình xử lý sự kiện. My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
94, My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95 và My First JavaScript says
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
96
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript có thể được sử dụng để xử lý nhiều loại sự kiện, để phản hồi hành động của người dùng hoặc hành động của trình duyệt. Ví dụ,
My First JavaScript says
94. kích hoạt sau khi trình duyệt tải trangHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
95 vàHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
96. kích hoạt khi người dùng trỏ con trỏ chuột vào/ra khỏi phần tử HTMLHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
Phân tích chương trình- Dòng 8 định nghĩa một biến
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
00, chứa các chuỗi sẽ được hiển thị trong trình xử lý sự kiệnMy First JavaScript says
94Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Trong thẻ mở [Dòng 12], chúng tôi xác định trình xử lý sự kiện
My First JavaScript says
94 cho sự kiệnHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
03. Nó gọifor [ initialization ; test ; post-processing ] { body ; }
8 với thông báo được xác định trước đó - Dòng 13 và 14 xác định trình xử lý sự kiện
My First JavaScript says
95 vàHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
96 cho phần tử HTMLHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Màu của văn bản sẽ được thay đổi thành màu đỏ khi người dùng trỏ con trỏ chuột vào phần tử [bằng cách đặt thuộc tính kiểu CSS
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
07 thành màu đỏ] và trở lại màu ban đầu khi con trỏ chuột được di chuyển ra xa [bằng cách đặt lại kiểu CSS . Từ khóa đặc biệtalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
09 đề cập đến đối tượng này
JS phía máy khách EG 7. Tách HTML, CSS và JavaScript
Ví dụ trước hoạt động tốt. Bạn sẽ tìm thấy nhiều ví dụ như vậy trong sách giáo khoa, đặc biệt là sách giáo khoa cũ. Tuy nhiên, nó có một vấn đề lớn. Tất cả nội dung HTML, kiểu trình bày CSS và mã lập trình JavaScript được đặt trong một tệp duy nhất. Đối với một chương trình đồ chơi nhỏ, vấn đề không nghiêm trọng. Nhưng khi chương trình của bạn phát triển và nếu HTML, CSS và JavaScript được viết bởi những người khác nhau, bạn sẽ gặp thách thức thực sự trong việc duy trì chương trình. Hãy nhớ rằng HTML dành cho nội dung, CSS dành cho trình bày và JavaScript dành cho hành vi
Hãy viết lại ví dụ để đặt HTML, CSS và JavaScript vào ba tệp khác nhau
My First JavaScript says0Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says1Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says2Làm thế nào nó hoạt động?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Đặt mã kịch bản bên trong trang HTML không phải là một thực hành kỹ thuật phần mềm tốt. Thay vào đó, cách tiếp cận được ưa thích hiện nay là đặt các tập lệnh, cũng như các kiểu CSS, trong các tệp bên ngoài, sau đó có thể áp dụng thống nhất cho tất cả các trang trong trang web của bạn
- Hãy bắt đầu với tệp HTML. Bây giờ, tệp HTML chỉ giữ nội dung, không có kiểu trình bày và logic lập trình
Biểu định kiểu CSS được lưu trong một tệp bên ngoài, được tham chiếu qua thẻ
6 ở trênJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Ghi chú. Trong HTML4/XHTML1. 0 bạn cần đưa thuộc tínhalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
11 vào thẻ mở
6JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Ngoài ra, mã lập trình JavaScript được giữ trong một tệp bên ngoài, cần được tham chiếu qua
6 ở trên, mặc dù không có nội dungJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Ghi chú. Trong HTML4/XHTML1. 0, bạn cần bao gồm thuộc tính
7 trongJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
6JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
My First JavaScript says
3Làm thế nào nó hoạt động?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Tài liệu HTML này chứa một phần tử với một
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
16 duy nhất, haiyếu tố, ba
phần tử với
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
17 và ba phần tử
6 với phần tử duy nhất làJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
19 - Trong hàm do người dùng định nghĩa
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
20, chúng tôi sử dụngalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
21 để chọn phần tử, sau đó sửa đổi thuộc tínhalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
22 của nó - Trong hàm
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
23, chúng tôi sử dụngalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
24 để chọn tất cảcác phần tử trong một mảng
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
25. Sau đó, chúng tôi sử dụng vòng lặp for để lặp qua tất cả các phần tử trong mảng. Thuộc tínhalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
26 giữ độ dài của mảng - Trong hàm
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
27, chúng tôi sử dụngalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
28 để chọn tất cảyếu tố. Chúng tôi sử dụng vòng lặp
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
29 của JavaScript để lặp qua tất cả các phần tử. Chúng tôi sử dụng biếnif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 an toàn nhất, vì vòng lặpalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
29 tạo một biến phạm vi khối mới cho mỗi lần lặp; - Trang chứa ba nút để kích hoạt các chức năng được xác định
- để thao tác các phần tử trong DOM bằng JavaScript, mã JavaScript phải được chạy sau khi phần tử liên quan đã được tạo trong tài liệu. Điều này có thể được thực hiện bằng cách đặt JavaScript ngay trước thẻ kết thúc
- Tập lệnh chứa một hàm
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33, được chỉ định làm trình xử lýMy First JavaScript says
94 thông quaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
35. Tức là,alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33 sẽ được kích hoạt sau khi trang được tải. Hàmalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33 gán trình xử lý sự kiệnMy First JavaScript says
82 cho các nút, được chọn quaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
39 bằng cách sử dụng các nút duy nhất '_______57_______19 - Thay vì thuộc tính
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
22, các phiên bản JavaScript cũ hơn sử dụngalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
42 [bao gồm cả văn bản và phần đánh dấu] để sửa đổi nội dung của phần tử được chọn. Theo MDN. "_______57_______22 nên được sử dụng thay thế. Do văn bản không được phân tích cú pháp dưới dạng HTML, nên văn bản có thể có hiệu suất tốt hơn. Hơn nữa, điều này tránh được vectơ tấn công XSS [Cross-Site Scripting]. "
JS phía máy khách EG 9. Chặn một siêu liên kết
Ví dụ này sử dụng tập lệnh chặn siêu liên kết để đưa ra thông báo cảnh báo, sau đó tiếp tục truy cập liên kết
My First JavaScript says
4Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
5Làm thế nào nó hoạt động?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Trang HTML có một siêu liên kết
6với mộtJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
19 duy nhất - Hàm
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
33 chỉ định trình xử lýMy First JavaScript says
82 cho siêu liên kết, được chọn quaHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
39 - Khi nhấp vào siêu liên kết, trình xử lý
My First JavaScript says
82Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
50 được kích hoạt. NếuMy First JavaScript says
16 trả về true, trang mới sẽ được tải;Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Các ví dụ JavaScript nâng cao khác
Thêm "Ví dụ JavaScript nâng cao"
Gỡ lỗi JavaScript
Một trình gỡ lỗi đồ họa là PHẢI trong lập trình. Đặc biệt, lập trình bằng JavaScript yêu cầu một trình sửa lỗi tốt. Đó là bởi vì JavaScript được giải thích và không cần phải biên dịch. Do đó, không có trình biên dịch nào chỉ cho bạn các lỗi cú pháp. Một lỗi cú pháp đơn giản và tầm thường [e. g. , thiếu dấu ngoặc, viết sai chính tả] sẽ khiến toàn bộ tập lệnh không thể thực hiện được. Tệ nhất là bạn sẽ không thấy bất kỳ thông báo lỗi nào khi tập lệnh không hoạt động. Bạn có thể gỡ lỗi mà không có bất kỳ thông báo lỗi hoặc manh mối nào không?
Nếu không có gỡ lỗi đồ họa, cách duy nhất để gỡ lỗi JavaScript là chèn
for [ initialization ; test ; post-processing ] { body ; }
8 vào các vị trí chiến lược để in ra dữ liệu đã chọnSau khi sửa đổi JavaScript, tôi khuyên bạn nên sử dụng Ctrl-F5 để làm mới trang, thao tác này sẽ tải một bản sao mới thay vì tải từ bộ đệm. Bạn có thể cần xóa bộ nhớ cache của trình duyệt hoặc khởi động lại trình duyệt nếu sửa đổi của bạn không có hiệu lực
Công cụ dành cho nhà phát triển web F12Trong Chrome và Firefox, bạn có thể nhấn F12 để kích hoạt Công cụ dành cho nhà phát triển [do đó được gọi là Công cụ dành cho nhà phát triển F12]
Nó cung cấp các tính năng này
- Bảng điều khiển. Xem thông báo lỗi JavaScript. Bắt đầu firebug và chuyển sang bảng điều khiển "Bảng điều khiển"
- Kịch bản. Xem và gỡ lỗi JavaScript. Bắt đầu con bọ lửa. Chuyển sang bảng "Script". "Bật" hoặc "Tải lại" nếu cần. Bạn có thể đặt điểm dừng bằng cách nhấp vào số câu lệnh, thực hiện một bước qua các câu lệnh JavaScript, xem biểu thức, v.v.
- DOM. Xem DOM HTML của tài liệu hiện tại
- HTML và CSS
Để gỡ lỗi JavaScript
- Khởi chạy Firebug ⇒ Chọn bảng "Tập lệnh" để xem JavaScript của bạn, được hiển thị bằng số dòng in đậm màu xanh lá cây
- Làm mới [Ctrl-F5] trang và kiểm tra bảng điều khiển lỗi [Trong "Bảng điều khiển" ⇒ "Lỗi"] để biết các lỗi cú pháp. Sửa tất cả các lỗi cú pháp
- Để theo dõi chương trình, hãy đặt các điểm dừng tại các câu lệnh JavaScript đã chọn, bằng cách nhấp vào lề phải [ở bên trái của số dòng]. Một vòng tròn màu đỏ xuất hiện biểu thị một điểm dừng. Lưu ý rằng bạn chỉ có thể đặt điểm dừng trên các câu lệnh có số dòng in đậm màu xanh lá cây. [Nếu các câu lệnh JavaScript của bạn không có số dòng in đậm màu xanh lá cây, thì có lỗi cú pháp trên các câu lệnh này. Bạn cần sửa lỗi cú pháp và tải lại trang. ]
- Kích hoạt tập lệnh [thông qua nhấp vào nút/liên kết hoặc tải lại trang]. Việc thực hiện dừng tại điểm dừng đầu tiên. Sau đó, bạn có thể chuyển qua câu lệnh [hoặc bước vào chức năng] và kiểm tra các biến bằng cách định vị con trỏ chuột trên biến;
- Bạn có thể tiếp tục thực hiện [thông qua nút tiếp tục]
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
53Thay vì sử dụng
for [ initialization ; test ; post-processing ] { body ; }
8 vàfor [ initialization ; test ; post-processing ] { body ; }
9 để hiển thị kết quả trung gian, bạn cũng có thể sử dụngalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
56, viếtalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
57 và giá trị củaalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
58 vào bảng điều khiển lỗi. Nó không can thiệp vào sự xuất hiện của trang web cũng như hoạt động bình thường của bạnVí dụ: sửa đổi JavaScript trong Ví dụ 8 để in các giá trị của InternalHTML trước khi sửa đổi. Bạn cần bật bàn điều khiển [nhấn F12] để xem đầu ra
My First JavaScript says
6Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript và HTML
Như đã thấy từ các ví dụ trước, JavaScript được nhúng bên trong tài liệu HTML và được trình duyệt thực thi. Có hai trường hợp mà trình duyệt thực thi các lệnh JavaScript
- JavaScript đính kèm trong
6 chạy trong khi tải trangJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- Một số JavaScript, được gọi là trình xử lý sự kiện, chạy do hành động [hoặc sự kiện] của người dùng hoặc trình duyệt. Ví dụ: nhấp vào nút [
My First JavaScript says
82] hoặc tải trang [Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
94]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Do đó, có hai nơi để đặt JavaScript của bạn
- giữa các thẻ chứa
6;JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- bên trong các thẻ HTML dưới dạng trình xử lý sự kiện [chẳng hạn như
My First JavaScript says
82,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
95,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
94], e. g. ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
66
6 chứa các câu lệnh lập trình JavaScript. Ví dụ,JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Trong HTML4/XHTML1. 0, bạn cần thêm thuộc tính
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
68 vào
6 là cần thiết mặc dù không có nội dung. ?JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
Trong HTML4/XHTML1. 0, bạn cần bao gồm
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
68 trong các thẻ
6 và chạy nó dưới trình duyệt web, bạn sẽ cần kích hoạt trình gỡ lỗi [Công cụ dành cho nhà phát triển web] để xem kết quả. Xem phần trên về các ví dụ phía máy kháchJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
JS độc lập Ví dụ 2. [LÀM]- Tài liệu HTML này chứa một phần tử với một
- Nhận xét HTML được đính kèm bên trong
- CSS hỗ trợ chú thích nhiều dòng
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
75, nhưng KHÔNG hỗ trợ chú thích cuối dòngMy First JavaScript says
49Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- JavaScript hỗ trợ cả
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
75 vàMy First JavaScript says
49, như Java/C/C++/C#Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Mã định danh có thể chứa các chữ cái [_______57_______92,
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
93], chữ số [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
94], dấu gạch dưới [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
95] và ký hiệu đô la [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
96]. Nhưng nó không thể bắt đầu bằng một chữ số [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
94] - Mã định danh phân biệt chữ hoa chữ thường. Một
0 KHÔNG phải là mộtJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
2, và KHÔNG phải là mộtJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
1JavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
- Mã định danh không thể là từ khóa
- Hãy lưu ý rằng KHÔNG được phép có dấu gạch ngang [
01] và khoảng trắng. Do đó,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
02,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
03, KHÔNG phải là định danh hợp lệ. Bạn nên sử dụng dấu gạch dưới [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
95] thay vì dấu gạch ngang [
01]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- sợi dây. một dãy ký tự. Các chuỗi ký tự được đặt trong một cặp dấu nháy đơn hoặc nháy kép [e. g. ,
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- con số. lấy cả hai số nguyên [e. g. ,
06] hoặc dấu phẩy động [e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
15]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- boolean. lấy nghĩa đen boolean của một trong hai
My First JavaScript says
26 hoặcHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
27 [viết thường]Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- không xác định. nhận một giá trị theo nghĩa đen đặc biệt được gọi là
18. Lưu ý rằngJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18 vừa là loại vừa là giá trị theo nghĩa đenJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- biểu tượng [ES6]. Một kiểu dữ liệu có thể hiện là duy nhất và không thay đổi
- bigint [ES2020/ES11]. số nguyên với độ chính xác tùy ý
- sự vật. cho các đối tượng chung
- chức năng. cho các đối tượng chức năng. Không giống như Java, hàm là một đối tượng hạng nhất trong JavaScript, e. g. , bạn có thể gán một hàm cho một biến
- vô giá trị. Một giá trị theo nghĩa đen đặc biệt cho chưa phân bổ [chưa xây dựng]
20. Hãy lưu ý rằngJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
21 KHÔNG phải làHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
22.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
23 làJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
20.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
21 có nghĩa là đại diện cho sự vắng mặt của một đối tượng được xây dựng.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21 thực sự được coi là nguyên thủy vì nó không có thuộc tính và phương thứcHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
- Số thập phân. bắt đầu bằng một chữ số
47 đếnJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
48 [không phảiJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
49], e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
50 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
51,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- bát phân. bắt đầu bằng một chữ số
49, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
53 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
54,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- thập lục phân. begins with
55 [orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
56], e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
57 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
58JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- nhị phân. begins with
59 [orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
60], e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
61 hoặcJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
62. [Nhị phân có thể không được hỗ trợ trong một số trình duyệt. ]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- infinity. e. g. ,
67JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- -infinity. e. g. ,
68JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- NaN [Not-a-Number]. e. g. ,
69, or converting the stringJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
70 to a numberJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- parseInt[str], parseFloat[str]. Parse the
3 until the first non-digit, and return the number; orJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
91JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Math. round[num], Math. floor[num], Math. ceil[num]
- Math. random[]. Generate a random number between 0 [inclusive] and 1 [exclusive]
- isNaN[str]. return true if the
3 is not a number. For example, It is interesting to note that JavaScript does not have counterpart functions likeJavaScript Example: Events onload, onmouseover and onmouseout
"Hello" alert Box appears after the page is loaded.
Point your mouse pointer here!!!
93,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
94JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- Number[str]. Return the number represented by
My First JavaScript says
25, orHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
91. Take that that this function name begins with uppercase, because this is a type casting operationJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- toFixed[decimalPlaces]. Return this number/string to the given number of decimal places. For example,
- length. E. g. ,
48 returns the length of the stringJavaScript Example: The Date object
- toUpperCase[]. returns the uppercase string
- toLowerCase[]. returns the lowercase string
- charAt[idx]. returns the character at the
49 position. Index begins from 0. Negative index can be used, which counts from the end of the stringJavaScript Example: The Date object
- substring[beginIdx, endIdx]. returns the substring from
50 [inclusive] toJavaScript Example: The Date object
51 [exclusive]JavaScript Example: The Date object
- substr[beginIdx, length]. returns the substring from
50 ofJavaScript Example: The Date object
53JavaScript Example: The Date object
- indexOf[searchStr, fromIdx?]. Return the beginning index of the first occurrence of
54, starting from an optionalJavaScript Example: The Date object
55 [default of 0]; orJavaScript Example: The Date object
56 if not foundJavaScript Example: The Date object
- lastIndexOf[searchStr, fromIdx?]. Return the beginning index of the last occurrence of
54JavaScript Example: The Date object
6, starting from an optionalJavaScript Example: User-defined function and title Event Handler
Example on event and user-defined function
55 [default ofJavaScript Example: The Date object
60]; orJavaScript Example: The Date object
56 if not foundJavaScript Example: The Date object
- slice[beginIdx, endIdx]. Return the substring from
62 [inclusive] toJavaScript Example: The Date object
63 [exclusive]JavaScript Example: The Date object
- repeat[count] [ES6]. repeat the string
- split[delimiter], array. join[delimiter]. returns an array by splitting the string using
64, e. g. ,JavaScript Example: The Date object
- Searching/Modifying Strings using Regular Expression. See ""
- charAt[idx]. For example,
- Array Bracket Operator []. You can treat a string as a character array, and access individual character via array bracket operator
65, e. g. ,JavaScript Example: The Date object
- number
49JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- number
91 [Not-a-Number],JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- empty string [
13,JavaScript Example: The Date object
88],JavaScript Example: The Date object
18 value,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
- boolean
My First JavaScript says
27,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21 [unallocated] objectHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
[TODO] Kiểm tra các Hạn chế như Tệp IO, Mạng, v.v. So sánh với Shell Script và Python script để tự động hóa
Ví dụ JavaScript phía máy chủ
Bạn cũng có thể sử dụng JavaScript để lập trình ứng dụng web phía máy chủ, giống như Java Servlet/JSP, Microsoft ASP, PHP, Python, v.v. Điểm hấp dẫn là bạn có thể sử dụng một ngôn ngữ duy nhất để lập trình cả phía máy khách và phía máy chủ trong ứng dụng web
Xem nút. bài viết js
Cú pháp cơ bản JavaScript
Bình luận
Các nhận xét bị thời gian chạy JavaScript bỏ qua nhưng rất hữu ích trong việc giải thích mã của bạn cho người khác [và cả cho chính bạn ba ngày sau]. Bạn nên sử dụng nhận xét một cách tự do để giải thích hoặc ghi lại mã của mình
Nhận xét cuối dòng bắt đầu bằng
My First JavaScript says
49 và kéo dài đến cuối dòng hiện tại. Nhận xét nhiều dòng bắt đầu bằngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
73 và kéo dài đếnalert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
74lưu ý rằng
Whitespaces [blank, tab, newline]
Like C/C++/Java, JavaScript ignores additional whitespaces [blanks, tabs, and newlines]. Tôi thực sự khuyên bạn nên sử dụng các khoảng trắng bổ sung để định dạng chương trình của mình nhằm làm cho mã của bạn dễ đọc và dễ hiểu hơn
Biểu thức
Một biểu thức là sự kết hợp của các biến, ký tự, toán tử và biểu thức con có thể được đánh giá để tạo ra một giá trị duy nhất
Câu lệnh, Dấu chấm phẩy và Khối
Một tuyên bố là một hướng dẫn lập trình duy nhất. Không giống như C/C++/Java, nơi bạn cần kết thúc câu lệnh bằng dấu chấm phẩy [
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
79], trong JavaScript, dấu chấm phẩy là tùy chọn. Tuy nhiên, nếu thiếu dấu chấm phẩy, bạn cần kết thúc câu lệnh bằng một dòng mới [và công cụ JavaScript sẽ chèn dấu chấm phẩy cho bạn]Dấu chấm phẩy trong JavaScript chia rẽ cộng đồng. Một số thích sử dụng chúng luôn luôn, không có vấn đề gì. Những người khác muốn tránh chúng cho ngắn gọn. tôi sẽ để nó cho bạn
Một khối bao gồm 0 hoặc nhiều câu lệnh được đặt trong một cặp dấu ngoặc nhọn
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
80. Không cần dấu chấm phẩy sau dấu ngoặc nhọnBiến, chữ và kiểu
Khai báo biến [My First JavaScript says
29,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8] và Toán tử gán [alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
84]Một biến là một vị trí lưu trữ được đặt tên chứa một giá trị. Trước ES6, bạn chỉ có thể sử dụng
My First JavaScript says
29 để khai báo biến toàn cục. ES6 giới thiệu hai từ khóa mới.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7 để khai báo một biến phạm vi khối cục bộ vàif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 để khai báo một hằng số phạm vi khối cục bộBạn nên thử
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 [an toàn nhất], tiếp theo làif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
7 và tránhMy First JavaScript says
29Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Bạn có thể gán [và gán lại] một giá trị cho một biến bằng toán tử gán [
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
84]. Ví dụ,định danhMã định danh là tên được đặt để xác định các thực thể [chẳng hạn như biến và hàm]. Các quy tắc cho số nhận dạng hợp lệ là
Một chữ là một giá trị cố định, e. g. ,
06,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
07,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
09,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
26,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21, có thể được gán cho một biến hoặc tạo thành một phần của biểu thứccác loạiHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
JavaScript là hướng đối tượng. Nhưng, Nó hỗ trợ cả các kiểu và đối tượng nguyên thủy
Nguyên thủy không phải là đối tượng và không sở hữu các thuộc tính và phương thức. JavaScript hỗ trợ các kiểu nguyên thủy này
JavaScript cũng hỗ trợ các loại đối tượng và giá trị này [chúng ta sẽ thảo luận về đối tượng sau]
Không giống như hầu hết các ngôn ngữ lập trình chung [chẳng hạn như Java/C/C++/C#] là loại mạnh, JavaScript là loại lỏng lẻo [tương tự như hầu hết các ngôn ngữ kịch bản như UNIX Shell Script, Perl, Python]. You do not have to explicitly declare the type of a variable [such as
27 andJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
28] during declaration. Loại được quyết định khi một giá trị được gán cho biến đó. Nếu một số được gán, thì biến đó có kiểuJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
74 và có thể thực hiện các phép tính số như cộng và trừ. Nếu một chuỗi được gán, biến đó sẽ có kiểuHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
30 và có thể thực hiện các thao tác chuỗi như nối chuỗi. Nói cách khác, loại được liên kết với giá trị, thay vì biếnNhà điều hànhJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
31JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Bạn có thể sử dụng toán tử
31 để kiểm tra loại giá trị hiện tại được gán cho một biến.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
31 trả về một chuỗiJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Ví dụ,
Loại
18 và giá trị theo nghĩa đen củaJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Một biến không được khai báo [thông qua từ khóa
My First JavaScript says
29] có một loại đặc biệt gọi làHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
18. Bạn không thể tham khảo giá trị của nóJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Khi một biến được khai báo [thông qua từ khóa
My First JavaScript says
29] mà không gán giá trị ban đầu, biến đó sẽ có kiểuHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
18 và giữ một giá trị đặc biệt có tên làJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
18 [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
41 có lẽ chính xác hơn?. ], ngay sau khi một giá trị được gán, biến sẽ nhận kiểu của giá trị đó. Hành động đặt một giá trị vào một biến đặt kiểu của nó. Bạn có thể thay đổi loại biến bằng cách gán lại giá trị của loại khác. Nói cách khác, loại được liên kết với giá trị, không phải biến. Các loại được chuyển đổi tự động khi cần trong quá trình thực thi [được gọi là kiểu động]JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Ví dụ như,
[ES6] Hằng sốif [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8-khai báoBạn có thể tạo một hằng số chỉ đọc, được đặt tên với từ khóa
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
8 [thay choMy First JavaScript says
29]. Ví dụ,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
7Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Loại
My First JavaScript says
74, Chữ & Hoạt độngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Một biến kiểu
My First JavaScript says
74 chứa một số, có thể là số nguyên hoặc số dấu phẩy độngHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Chữ số nguyên có thể được biểu thị bằng
Floating-point literals can be expressed in the usual form [e. g. ,
63] or scientific notation, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
64,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
65JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
JavaScript also provides some special
My First JavaScript says
74 literalsHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Arithmetic operations, as tabulated below, can be applied to numbers. The following results are obtained assuming that
71,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
72 before the operationOperatorDescriptionExampleResult [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
71,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
72]+Additionz = x + y;z is 7-Subtraction [or Unary Negation]z = x - y;z is 3*Multiplicationz = x * y;z is 10/Divisionz = x / y;z is 2. 5%Modulus [Division Remainder]z = x % y;z is 1++Unary Pre- or Post-Incrementy = x++; z = ++x;JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Same as. y = x; x = x+1; x = x+1; z = x;y is 5; z is 7; x is 7--Unary Pre- or Post-Decrementy = --x; z = x--;
Same as. x = x-1; y = x; z = x; x = x-1;y is 4; z is 4; x is 3**Exponent
[ES7]y ** xIn JavaScript, arithmetic operations are always performed in double-precision floating-points [NOT integers]. That is,
75 givesJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
76 [instead of 0 in Java/C/C++]. You may use the built-in functionJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
77 to truncate a floating-point value to an integer, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
78 andJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
79 givesJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
80. You may also use the built-in mathematical functions such asJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
81,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
82,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
83 for converting a floating-point number to an integerExponent Operator [JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
0]JavaScript Example: Loop
Testing Loop
ECMAScript 2016 [ES7] introduces exponent operator [
0], you can writeJavaScript Example: Loop
Testing Loop
86, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
87JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Before ES7, you have to use built-in function
88Arithmetic cum Assignment OperatorsJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
These are short-hand operators to combine two operations
OperatorDescriptionExampleResult+=Addition cum Assignmentx += y;Same as. x = x + y;-=Subtraction cum Assignmentx -= y;Same as. x = x - y;*=Multiplication cum Assignmentx *= y;Same as. x = x * y;/=Division cum Assignmentx /= y;Same as. x = x / y;%=Modulus cum Assignmentx %= y;Same as. x = x % y;Some frequently-used
89 built-in FunctionsJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
The
30 Type, Literals & OperationsJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
A
30 is a sequence of characters enclosed within a pair of single quotes or double quotes [e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
01,JavaScript Example: The Date object
02]. ES6 introduces multi-line template strings delimited by back quotes [in the next section]. You can use an escape sequence to represent special non-printable characters [such asJavaScript Example: The Date object
My First JavaScript says
58 for new-line,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
04 for tab, andJavaScript Example: The Date object
05 for Unicode character]; and to resolve conflict [e. g. ,JavaScript Example: The Date object
06,JavaScript Example: The Date object
07,JavaScript Example: The Date object
08]JavaScript Example: The Date object
Unlike Java/C/C++, but like HTML/CSS's attributes, you can use either single quotes or double quotes for
30. This is handy as you can use single quotes if theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 contains double quotes [e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
11JavaScript Example: The Date object
11], without using the clumsy escape sequences [e. g. ,JavaScript Example: The Date object
13]JavaScript Example: The Date object
JavaScript is dynamically-type, and performs type conversion automatically. When a
30 value is used in arithmetic operations [such as subtraction or multiplication], JavaScript runtime automatically converts theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 to aJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
74 if it represents a validHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
74; or a specialHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
74 calledHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
91 [not-a-number] otherwise. For example,TheJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
00 Operator. Addition or Concatenation?Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
If both the operands to a
My First JavaScript says
00 operator areHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
22, it performs the usual numeric addition. However, if one [or both] of the operand is aJavaScript Example: The Date object
30, theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
00 operator is overloaded to perform string concatenation. The other operand will be converted to aHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
30, if necessary. For example,Converting a numericJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 toJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
74.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
77,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
29 andJavaScript Example: The Date object
30JavaScript Example: The Date object
To convert a numeric
30 to aJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
74, you could use the built-in functionsHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
77 orJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
29, which returns aJavaScript Example: The Date object
My First JavaScript says
74 if conversion is successful; orHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
91 otherwise. For example,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
Take note that
77 works as long as theJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30 begins with digits. It will parse up to the first non-digit. For example,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
You can also use the function
30, which converts the object argument to a number that represents the object's value; orJavaScript Example: The Date object
91 if conversion fails. For example,Constructing/Converting toJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
30.JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
42 andJavaScript Example: The Date object
43JavaScript Example: The Date object
You can construct a
30 from another type viaJavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
42 function, e. g. ,JavaScript Example: The Date object
You can use
43 to obtain aJavaScript Example: The Date object
30 representation of an object, e. g. ,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
You can also convert a number to string by concatenating with an empty string, e. g. ,
String's PropertiesJavaScript does not have a dedicated character type, but treats it as a one-character single. To get an individual character from a string, you can use
[ES6] Multi-line Back-Quoted Template String and Substitution
Prior to ES6, You can enclosed a string literal using either single or double quotes [e. g. ,
08,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
01,JavaScript Example: The Date object
02]. Escape sequences are to be used for special characters [e. g. ,JavaScript Example: The Date object
My First JavaScript says
58 cho dòng mới,Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
04 cho tab] và giải quyết xung đột [e. g. ,JavaScript Example: The Date object
06,JavaScript Example: The Date object
07,JavaScript Example: The Date object
08]. Multi-line strings are not supportedJavaScript Example: The Date object
ES6 introduces template string, delimited by back-quotes, for example,
There is no need to escape single or double quote inside a template string [because there is no conflicts]. But you need to use escape sequence for back-quote, i. e. ,
75JavaScript Example: The Date object
Chuỗi mẫu hỗ trợ chuỗi nhiều dòng. Tất cả các khoảng trắng [trống, tab, dòng mới] trong dấu ngoặc kép là một phần của chuỗi nhiều dòng. Ví dụ,
Thay thế
76JavaScript Example: The Date object
Chuỗi mẫu hỗ trợ thay thế, cho phép bạn nhúng bất kỳ biểu thức JavaScript hợp lệ nào như một phần của chuỗi ở dạng
76, ví dụ:JavaScript Example: The Date object
Chuỗi mẫu nhiều dòng có thay thế cung cấp một cách thuận tiện hơn để viết ra đoạn HTML được định dạng. Ví dụ,
My First JavaScript says
8Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
78 Loại, Nghĩa đen & Hoạt độngJavaScript Example: The Date object
Một biến kiểu
78 giữ giá trị làJavaScript Example: The Date object
My First JavaScript says
26 hoặc làHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
27.Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
26 vàHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
27 là từ khóa trong JavaScriptHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
Như đã đề cập, JavaScript thực hiện chuyển đổi kiểu tự động nếu cần thiết. During the type conversion, the following 6 values are converted to
My First JavaScript says
27Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
All the other values are converted to
My First JavaScript says
26Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
You need to memorize this.
93 returnJavaScript Example: The Date object
My First JavaScript says
26 ifHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
95 is one of these 6 values.JavaScript Example: The Date object
49,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
91,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
13,JavaScript Example: The Date object
18,JavaScript Example: Variables and functions prompt[] and confirm[]
Welcome to JavaScript!
My First JavaScript says
27, andHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
My First JavaScript says
21Comparison [Relational] OperatorsHello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
The following relational operators, which produce a
78 result, are defined in JavaScript. The results are obtained assumingJavaScript Example: The Date object
if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
03,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
04OperatorDescriptionExampleResult
[if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
03,if [ condition-1 ] { block-1 ; } else if [ condition-2 ] { block-2 ; } else if [ condition-3 ] { block-3 ; ...... ...... } else { block-n ; }
04]==Abstract Equal To
[in Value]My First JavaScript says
9Hello world, again!
This document was last modified on mm/dd/yyyy hh:mm:ss.
alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
0. =Abstract Not Equal To===Strictly Equal To
[in Type and Value]alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
1alert['hello, world'] document.write['hello world, again'] console.log['hello world, again and again']
2. ==Strictly Not Equal To>Greater Than>=Greater Than or Equal To