Javascript kiểm tra xem Internalhtml có chứa chuỗi không

Để biết liệu một Chuỗi cụ thể có chứa một từ đã cho hay một chuỗi con hay không, bạn có thể sử dụng phương thức indexOf() trong JavaScript

Show

Chuỗi. nguyên mẫu. Chỉ số(). The indexOf method will return the index of the calling String object, it will return the first occurrence of the provided substring value. It will return -1 if the provided substring is not found

Example.

JavaScript Substring example:

This is some text and i want to find if the work apple is in this string or not!

This string does not contains that word!
var val1 = document.getElementById("myString1").innerHTML.indexOf("apple");

var val2 = document.getElementById("myString2").innerHTML.indexOf("apple");

if(val1 > -1) {
  console.log("myString1 contains the word apple!");
} else {
  console.log("myString1 does not contain the word apple!")
}

if(val2 > -1) {
  console.log("myString2 contains the word apple!");
} else {
  console.log("myString2 does not contain the word apple!")
}

Output
"myString1 contains the word apple. "
"myString2 does not contain the word apple. "

Javascript kiểm tra xem Internalhtml có chứa chuỗi không

There are four parts in a for-loop. Three of them, initialization, test and post-processing, are enclosed in brackets

My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

73 and separated by 2 semicolons. The body contains the repetitive task to be performed. The initialization statement is first executed. The test is then evaluated. If the test returns true, the body is executed; followed by the post-processing statement. The test is evaluated again and the process repeats until the test returns false. When the test is false, the for-loop completes and program execution continues to the next statement after the for-loop. The following flow chart illustrates the for-loop process

In this example, the variable

My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 is initialized to 1. If
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 is less than or equal to 100, the body of the loop executes, followed by the post-processing statement, which increment the value of
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 by 1. The loop repeats until the value of
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

74 is NOT less than or equal to 100 (i. e. , more than 100)

TRY
  1. Modify the above script to prompt the user for the multiplier as well as the number of multiples to be printed (in two
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    15 statements)
  2. Modify the above script to print only multiples that are odd number. (Hint. The modulo operator "
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    79" can be used to compute the remainder, e. g. ,
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    80 computes the remainder of
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    81 divides by 2, which results in either 0 or 1. )

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

Bên cạnh các hàm có sẵn trong JavaScript như

for ( initialization ; test ; post-processing ) {
   body ;
}
8,
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.

85 và
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

57, 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ọi





  
  JavaScript Example: User-defined function and onclick 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
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    87, thông qua từ khóa
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    88. The function invokes the built-in function
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    89, which opens a new browser window (or tab) and loads the page "
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    90"
  • Dòng 15-16 tạo nút HTML. Clicking the button triggers the
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    82 event handler, i. e. ,
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    87 được xác định trước đó

CỐ GẮNG. Include another button, which opens "

My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

93"

Client-Side JS 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 and
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

96

JavaScript can be used to handle many types of events, in response to a user's action or browser's action. For example,

  • My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    94. fires after browser loaded the page
  • My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    95 and
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    96. fires when the user points the mouse pointer at/away from the HTML element




  
  JavaScript Example: Events onload, onmouseover and onmouseout
  


  

"Hello" alert Box appears after the page is loaded.

Point your mouse pointer here!!!

Dissecting the Program
  • Line 8 defines a variable
    alert('hello, world')
    document.write('hello world, again')
    console.log('hello world, again and again')
    00, which holds the strings to be displayed in the
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    94 event handlers
  • In the opening tag (Line 12), we define the
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    94 event handler for the
    alert('hello, world')
    document.write('hello world, again')
    console.log('hello world, again and again')
    03 event. It invokes
    for ( initialization ; test ; post-processing ) {
       body ;
    }
    8 with the message defined earlier
  • Line 13 and 14 defines the event handlers
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    95 and
    My First JavaScript says
    

    Hello world, again!

    This document was last modified on mm/dd/yyyy hh:mm:ss.

    96 for the HTML element

    The text's color will be changed to red when the user points the mouse pointer at the element (by setting the CSS style property

    alert('hello, world')
    document.write('hello world, again')
    console.log('hello world, again and again')
    07 to red), and revert back to its original color when the mouse pointer is moved away (by resetting the CSS style property
    alert('hello, world')
    document.write('hello world, again')
    console.log('hello world, again and again')
    07 to an empty string). The special keyword
    alert('hello, world')
    document.write('hello world, again')
    console.log('hello world, again and again')
    09 refer to this object

Client-Side JS EG 7. Separating HTML, CSS and JavaScript

The previous example works fine. You will find many such example in textbooks, especially the older textbooks. However, it has a big problem. All the HTML contents, CSS presentation styles and JavaScript programming codes are placed in a single file. For a small toy program, the problem is not serious. But when your program grows and if the HTML, CSS and JavaScript are written by different people, you will have a real challenge in maintaining the program. Remember that HTML is for the contents, CSS for presentation and JavaScript for the behavior

Let's rewrite the example to place the HTML, CSS and JavaScript in three different files

My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

0
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

1
My First JavaScript says

Hello world, again!

This document was last modified on mm/dd/yyyy hh:mm:ss.

2
Làm thế nào nó hoạt động?
  1. Placing the scripting codes inside the HTML page is not a good software engineering practice. Instead, the now-preferred approach is to place the scripts, as well as CSS styles, in external files, which can then uniformly applied to all pages in your website
  2. Let's begin with the HTML file. Now, the HTML file keeps only the contents, no presentation style and nor programming logic

    1. The CSS style sheet is kept in an external file, referenced via the above
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      6 tag
      Note. In HTML4/XHTML1. 0 you need to include attribute
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      11 in the
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      6 opening tag

    2. Also, the JavaScript programming code is kept in an external file, referenced via the above
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      6 is needed, although there is no content
      Note. In HTML4/XHTML1. 0, you need to include attribute
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      7 in the
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      6
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      3
      How it works?
      1. This HTML document contains a element with an unique
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        16, two

        elements, three

        elements with

        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        17, and three
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        6 with unique
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        19

      2. In the user-defined function
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        20, we use
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        21 to select the element, and then modify its
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        22 property
      3. In
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        23 function, we use
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        24 to select all the

        elements in an array alert('hello, world') document.write('hello world, again') console.log('hello world, again and again')25. We then use a for-loop to iterate through all the elements in the array. The alert('hello, world') document.write('hello world, again') console.log('hello world, again and again')26 property keeps the length of the array

      4. In
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        27 function, we use
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        28 to select all the

        yếu tố. We use JavaScript's

        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        29 loop to iterate through all the elements. We use the safest
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        8-variable, as
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        29 loop create a new block-scope variable for each iteration; but the traditional
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        32 does not

      5. The page contains three buttons to trigger the functions defined
      6. in order to manipulate elements in the DOM using JavaScript, the JavaScript code must be run after the relevant element has been created in the document. This can be done by putting the JavaScript just before the ending tag
      7. The script contains a function
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        33, which is assigned as the
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        94 handler via
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        35. That is,
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        33 will be triggered after the page is loaded. The
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        33 function assigns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82 event handlers to the buttons, selected via
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        39 using the unique buttons'
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        19
      8. Instead of
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        22 property, older versions of JavaScript use
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        42 (which includes both texts and markups) to modify the content of the selected element. According to MDN. "
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        22 should be used instead. Because the text is not parsed as HTML, it's likely to have better performance. Moreover, this avoids an XSS (Cross-Site Scripting) attack vector. "

      Client-Side JS EG 9. Chặn một siêu liên kết

      This example uses a script to intercept a hyperlink to put up a warning message, then proceed to the link

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      4
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      5
      How it works?
      1. The HTML page has an hyperlink
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        6with an unique
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        19
      2. The
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        33 function assigns an
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82 handler to the hyperlink, selected via
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        39
      3. When the hyperlink is clicked, the
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82 handler
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        50 triggered. If
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        16 returns true, the new page will be loaded; otherwise, the current page remains

      More Advanced JavaScript Examples

      More "Advanced JavaScript Examples"

      Debugging JavaScripts

      A graphic debugger is a MUST in programming. Programming in JavaScript, in particular, requires a good debugger. It is because JavaScripts are interpreted and does not need to be compiled. Hence, there is no compiler to show you the syntax errors. A simple and trivial syntax error (e. g. , missing bracket, mis-spelling) will render the entire script not workable. Worst still, you would not see any error message when the script did not work. Can you debug without any error message or clue?

      Without a graphic debugging, the only mean to debug JavaScript is to insert

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      8 at strategic locations to print out selected data

      After modifying a JavaScript, I recommend that you use Ctrl-F5 to refresh the page, which shall load a fresh copy instead of loading from the cache. 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

      F12 Web Developer Tools

      In Chrome and Firefox, you can press F12 to activate the developer Tools (hence called F12 Developer Tools)

      It provides these features

      1. Console. View the JavaScript error messages. Start the firebug and switch to the "Console" panel
      2. Script. View and debug 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.
      3. DOM. Xem DOM HTML của tài liệu hiện tại
      4. HTML và CSS

      Để gỡ lỗi JavaScript

      1. 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
      2. 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
      3. Để 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). A red circle shows up denoting a breakpoint. Take note that you can only set breakpoint on statements with a green-bold line number. [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. ]
      4. 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;
      5. 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')
      53

      Thay 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ụng
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      56, viết
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      57 và giá trị của
      alert('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ạn

      Ví 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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      6

      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

      1. JavaScript đính kèm trong
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        6 chạy trong khi tải trang
      2. 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
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82) hoặc tải trang (
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        94)

      Do đó, có hai nơi để đặt JavaScript của bạn

      1. giữa các thẻ chứa
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        6;
      2. 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
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        82,
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        95,
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        94), e. g. ,
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        66
      Phần tử
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      6 chứa các câu lệnh lập trình JavaScript. Ví dụ,

      Trong HTML4/XHTML1. 0, bạn cần bao gồm thuộc tính

      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      68 trong
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      6 là cần thiết mặc dù không có nội dung. ?

      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ẻ
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      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ách

    3. JS độc lập Ví dụ 2. [LÀM]

      [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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      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ằng
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      73 và kéo dài đến
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      74

      lưu ý rằng

      • 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òng
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        49
      • 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
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        49, như Java/C/C++/C#

      Khoảng trắng (trống, tab, dòng mới)

      Giống như C/C++/Java, JavaScript bỏ qua các khoảng trắng bổ sung (khoảng trống, tab và dòng mới). 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ọn

      Biến, chữ và kiểu

      Khai báo biến (
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29,
      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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29 để khai báo biến toàn cục. ES6 giới thiệu hai từ khóa mới.
      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ánh
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29

      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 danh

      Mã đị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ã định danh có thể chứa các chữ cái (_______59_______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
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        0 KHÔNG phải là một
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        2, và KHÔNG phải là một
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        1
      • 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 (
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        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 (
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        95) thay vì dấu gạch nối (
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        01)
      chữ

      Một chữ là một giá trị cố định, e. g. ,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      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,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      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ức

      các loại

      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

      1. chuỗi. 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. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        08,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        13)
      2. số. lấy cả hai số nguyên (e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        06) hoặc dấu phẩy động (e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        15)
      3. boolean. lấy nghĩa đen boolean của một trong hai
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 hoặc
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        27 (viết thường)
      4. chưa xác định. nhận một giá trị theo nghĩa đen đặc biệt được gọi là
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18. Lưu ý rằng
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18 vừa là loại vừa là giá trị theo nghĩa đen
      5. symbol (ES6). Một kiểu dữ liệu có thể hiện là duy nhất và không thay đổi
      6. bigint (ES2020/ES11). số nguyên với độ chính xác tù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)

      1. mục tiêu. cho các đối tượng chung
      2. hàm số. 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
      3. 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)
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        20. Hãy lưu ý rằng
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21 KHÔNG phải là
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        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.
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        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.
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21 thực sự được coi là nguyên thủy vì nó không có thuộc tính và phương thức

      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). Bạn không cần phải khai báo rõ ràng loại biến (chẳng hạn như

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      27 và
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      28) trong khi khai báo. 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ểu
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 và có thể thực hiện các phép toán số như cộng và trừ. Nếu một chuỗi được gán, biến đó sẽ có kiểu
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      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ến

      Người điều hành
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      31

      Bạn có thể sử dụng toán tử

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      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ỗi

      Ví dụ,

      Loại
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18 và giá trị theo nghĩa đen của
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18

      Một biến không được khai báo (thông qua từ khóa

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29) có một loại đặc biệt gọi là
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18. Bạn không thể tham khảo giá trị của nó

      Khi một biến được khai báo (thông qua từ khóa

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29) mà không gán giá trị ban đầu, nó sẽ có kiểu
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      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)

      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áo

      Bạ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 cho
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      29). Ví dụ,

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      7

      Loại
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74, Chữ & Hoạt động

      Một biến kiểu

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 chứa một số, có thể là số nguyên hoặc số dấu phẩy động

      Chữ số nguyên có thể được biểu thị bằng

      • Số thập phân. bắt đầu bằng một chữ số
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        47 đến
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        48 (không phải
        
        
        
        
          
          JavaScript 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ặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        51,
      • bát phân. bắt đầu bằng một chữ số
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        49, e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        53 hoặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        54,
      • thập lục phân. bắt đầu bằng
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        55 (hoặc
        
        
        
        
          
          JavaScript 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ặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        58
      • nhị phân. bắt đầu bằng
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        59 (hoặc
        
        
        
        
          
          JavaScript 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ặc
        
        
        
        
          
          JavaScript 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. ]

      Các chữ số dấu phẩy động có thể được biểu diễn ở dạng thông thường (e. g. ,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      63) hoặc ký hiệu khoa học, 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!

      65

      JavaScript cũng cung cấp một số

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 chữ đặc biệt

      • vô cực. e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        67
      • -vô cực. e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        68
      • NaN (Không phải là số). e. g. ,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        69 hoặc chuyển đổi chuỗi
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        70 thành một số
      Các phép tính toán học

      Các phép toán số học, như được lập bảng dưới đây, có thể được áp dụng cho các số. Các kết quả sau đây thu được với giả định rằng

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      71,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      72 trước khi phẫu thuật

      OperatorDescriptionExampleResult (
      
      
      
      
        
        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 (Phần còn lại của phép chia)z = x % y;z is 1++Unary Pre- or Post-Incrementy = x++;
      Giống như. y = x;
      Giống như. x=x-1;
      (ES7)y ** x

      Trong JavaScript, các phép toán số học luôn được thực hiện ở dạng dấu phẩy động có độ chính xác kép (KHÔNG phải là số nguyên). Tức là,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      75 cho ____76_______76 (thay vì 0 trong Java/C/C++). Bạn có thể sử dụng hàm có sẵn
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      77 để cắt một giá trị dấu phẩy động thành một số nguyên, e. g. ,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      78 và
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      79 mang lại cho
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      80. Bạn cũng có thể sử dụng các hàm toán học có sẵn như
      
      
      
      
        
        JavaScript 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 để chuyển đổi một số dấu phẩy động thành một số nguyên

      Toán Tử Số Mũ (
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      0)

      ECMAScript 2016 (ES7) giới thiệu toán tử số mũ (

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      0), bạn có thể viết
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      86, e. g. ,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      87

      Trước ES7, bạn phải sử dụng chức năng tích hợp sẵn

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      88

      Toán tử số học kiêm toán tử gán

      Đây là những toán tử tay ngắn để kết hợp hai phép toán

      Toán tử Mô tảVí dụResult+=Bổ sung kiêm Phép gánx += y;Giống như. x = x + y;-=Phép trừ kiêm phép tínhx -= y;Giống như. x = x - y;*=Phép nhân kiêm phép tínhx *= y;Giống như. x = x * y;/=Phép chia kiêm phép tínhx /= y;Giống như. x = x / y;%=Mô-đun kiêm Bài tậpx %= y;Giống như. x = x % y;
      Một số Hàm tích hợp
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      89 thường được sử dụng
      • parseInt(str), parseFloat(str). Phân tích cú pháp
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        3 cho đến số không phải chữ số đầu tiên và trả về số;
      • Toán học. vòng(số), Toán. sàn(số), Toán. trần nhà (số)
      • Toán học. ngẫu nhiên(). Tạo một số ngẫu nhiên giữa 0 (bao gồm) và 1 (độc quyền)
      • isNaN(str). trả về true nếu
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        3 không phải là số. Ví dụ: Thật thú vị khi lưu ý rằng JavaScript không có các chức năng đối ứng như
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        93,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        94
      • Số(str). Trả về số được đại diện bởi
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        25 hoặc
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        91. Cho rằng tên hàm này bắt đầu bằng chữ hoa, bởi vì đây là thao tác truyền kiểu
      • toFixed(decimalPlaces). Trả lại số/chuỗi này về số vị trí thập phân đã cho. Ví dụ,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 Loại, Chữ & Hoạt động

      Một

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 là một dãy các ký tự được đặt trong một cặp dấu nháy đơn hoặc nháy kép (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: The Date object
        
      
      
      
      01,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      02). ES6 giới thiệu các chuỗi mẫu nhiều dòng được phân tách bằng dấu ngoặc kép (trong phần tiếp theo). Bạn có thể sử dụng một chuỗi thoát để thể hiện các ký tự đặc biệt không in được (chẳng hạn như
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      58 cho dòng mới,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      04 cho tab và
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      05 cho ký tự Unicode); . g. ,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      06,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      07,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      08)

      Không giống như Java/C/C++, nhưng giống như các thuộc tính của HTML/CSS, bạn có thể sử dụng dấu ngoặc đơn hoặc dấu ngoặc kép cho

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30. Điều này rất tiện lợi vì bạn có thể sử dụng dấu nháy đơn nếu
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 chứa dấu nháy kép (e. g. ,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      11

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      11), mà không sử dụng trình tự trốn thoát vụng về (e. g. ,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      13)

      JavaScript là kiểu động và tự động thực hiện chuyển đổi kiểu. Khi giá trị

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 được sử dụng trong các phép toán số học (chẳng hạn như phép trừ hoặc phép nhân), thời gian chạy JavaScript sẽ tự động chuyển đổi ______76_______30 thành ____46_______74 nếu nó đại diện cho một ____46_______74 hợp lệ; . Ví dụ,

      Toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      00. Bổ sung hoặc nối?

      Nếu cả hai toán hạng của toán tử

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      00 là
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      22, nó sẽ thực hiện phép cộng số thông thường. Tuy nhiên, nếu một (hoặc cả hai) toán hạng là
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30, thì toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      00 bị quá tải để thực hiện nối chuỗi. Toán hạng khác sẽ được chuyển đổi thành
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30, nếu cần. Ví dụ,

      Chuyển đổi một số
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 thành
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74.
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      77,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      29 và
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      30

      Để chuyển đổi một số

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 thành một số
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74, bạn có thể sử dụng các hàm tích hợp sẵn
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      77 hoặc
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      29, hàm này sẽ trả về một số
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 nếu chuyển đổi thành công; . Ví dụ,

      Hãy lưu ý rằng

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      77 hoạt động miễn là
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 bắt đầu bằng các chữ số. Nó sẽ phân tích đến chữ số đầu tiên. Ví dụ,

      Bạn cũng có thể sử dụng hàm

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      30 để chuyển đổi đối số đối tượng thành một số đại diện cho giá trị của đối tượng; . Ví dụ,

      Xây dựng/Chuyển đổi thành
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      42 and
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      43

      Bạn có thể xây dựng một

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 từ một loại khác thông qua hàm
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      42, e. g. ,

      Bạn có thể sử dụng

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      43 để có được một đại diện
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 của một đối tượng, e. g. ,

      Bạn cũng có thể chuyển đổi một số thành chuỗi bằng cách nối với một chuỗi rỗng, e. g. ,

      Thuộc tính của chuỗi
      • chiều dài. e. g. ,
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        48 trả về độ dài của chuỗi
      Hoạt động của chuỗi
      • Đến trường hợp trên(). trả về chuỗi chữ hoa
      • toLowerCase(). trả về chuỗi chữ thường
      • charAt(idx). trả về ký tự ở vị trí
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        49. Index begins from 0. Negative index can be used, which counts from the end of the string
      • substring(beginIdx, endIdx). returns the substring from
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        50 (inclusive) to
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        51 (exclusive)
      • substr(beginIdx, length). returns the substring from
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        50 of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        53
      • indexOf(searchStr, fromIdx?). Return the beginning index of the first occurrence of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        54, starting from an optional
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        55 (default of 0); or
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        56 if not found
      • lastIndexOf(searchStr, fromIdx?). Return the beginning index of the last occurrence of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        54
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        6, starting from an optional
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        55 (default of
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        60); or
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        56 if not found
      • slice(beginIdx, endIdx). Return the substring from
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        62 (inclusive) to
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        63 (exclusive)
      • repeat(count) (ES6). repeat the string
      • split(delimiter), array. join(delimiter). returns an array by splitting the string using
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        64, e. g. ,
      • Searching/Modifying Strings using Regular Expression. See ""
      Character Type?

      JavaScript 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

      • charAt(idx). For example,
      • Array Bracket Operator []. You can treat a string as a character array, and access individual character via array bracket operator
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        65, e. g. ,

      (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. ,

      
      
      
      
        
        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: The Date object
        
      
      
      
      01,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      02). Escape sequences are to be used for special characters (e. g. ,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      58 for new-line,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      04 for tab) and resolving conflict (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 supported

      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. ,

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      75

      Template strings support multi-line strings. All whitespaces (blank, tab, newline) within the back-quotes are part of the multi-line string. For example,

      Substitution
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      76

      Template strings support substitution, which allow you to embed any valid JavaScript expression as part of the string in the form of

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      76, for example,

      Multi-line template string with substitution provides a more convenient way to write out formatted HTML fragment. For example,

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      8

      The
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 Type, Literals & Operations

      A variable of the type

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 holds a value of either
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 of
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27.
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 and
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27 are keywords in JavaScript

      As mentioned, JavaScript performs automatic type conversion if necessary. During the type conversion, the following 6 values are converted to

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27

      1. number
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        49
      2. number
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        91 (Not-a-Number),
      3. empty string (
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        13,
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        88),
      4. 
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18 value,
      5. boolean
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        27,
      6. My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21 (unallocated) object

      All the other values are converted to

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26

      You need to memorize this.

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      93 return
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 if
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      95 is one of these 6 values.
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      49,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      91,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      13,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27, and
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21

      Comparison (Relational) Operators

      The following relational operators, which produce a

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 result, are defined in JavaScript. The results are obtained assuming
      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

      OperatorDescriptionExampleResult
      (
      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
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      9
      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')
      1
      alert('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
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      07 vs.
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      08

      The strictly equality operator (

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      07) returns
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 if both operands have the same type and same value; while abstract equality operator (
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      08) returns
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 if both operands have the same value, but may or may not be the same type, i. e. , == converts its operands if the operand types do not match

      JavaScript needs to provide two different equality operators, because it is loosely type and carries out automatic type conversion in many situation. For example,

      • When a number is compared with a string, the string is converted to a number (or
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        91 if it does not contain a valid number). Hence,
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        14 returns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26. But,
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        16 returns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        27, because the operands are not of the same type
      • if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        07,
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        19,
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        08,
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        21 can be applied to boolean (and all the JavaScript types), e. g. ,
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        22 gives
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 (because empty string is converted to
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        27); but
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        25 gives
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26
      • It is RECOMMENED to use
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        07 (or
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        19), instead of
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        08 (or
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        21), unless you are certain that type is not important
      String Sorting (Collation) Order

      When two strings are compared, the encoding order (ASCII/Unicode table) is used. Hence, string

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      31 is greater than string
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      32

      Ví dụ,

      To compare two strings alphabetically, use

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      33, e. g. , [TODO]

      Logical (Boolean) Operators

      The following boolean (or logical) operators are provided in JavaScript

      OperatorDescriptionExampleResult&&Logical AND. Logical OR. Logical NOT&&=Logical AND cum assignment (ES12). =Logical OR cum assignment (ES12)

      Notes.

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      34 in JavaScript is a bitwise XOR operator, not really boolean XOR (which is rarely needed)

      Short-Circuit Evaluation for Boolean Expressions

      Evaluation of logical operations are always short-circuited. That is, the operation is terminated as soon as the result is certain, e. g. , (

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      35) is short-circuited to give
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27, (
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      37) gives
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26, the
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      5 will not be evaluated

      Explicit Type Conversion

      The JavaScript runtime performs type conversion automatically. However, at times, you may need to convert from one type to another explicitly

      Converting a

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 to a
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30. Simply concatenate the
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 with an empty
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      43, e. g. ,
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      44 gives
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      45

      Converting a

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 to a
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74. Use built-in functions
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      48,
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      49 or
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      50 to convert a
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      3 which contains a valid
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74. For example,
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      53 gives
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      80,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      78 gives
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      80,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      79 gives
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      80,
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      59 gives
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      60,
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      61 gives
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      80, but
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      63 gives
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      91

      Converting a float to an integer. Use

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      77 (e. g. ,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      78 gives
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      80), or built-in mathematical functions such as
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      81,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      83 or
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      82

      Flow Control - Decision (Conditional)

      JavaScript provides these flow control construct. The syntax is the same as Java/C/C++

      SyntaxExample
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      3
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      4
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      5
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      6
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      7

      Notes. JavaScript, like C/Java, does not support native

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      71, but nested
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      72

      [ES12] Logical nullish Assignment Operator (
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      5)

      The logical nullish assignmebnt

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      74 assigns only if x is
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21 (unallocated object) or
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18 (uninitialized variable) i. e. , nullish. For example,

      Flow Control - Loops (Repetition)

      JavaScript cung cấp các cấu trúc vòng lặp sau. Cú pháp giống như Java/C/C++

      JavaScript cũng giới thiệu các vòng lặp

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      77 và
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      29 (sẽ được thảo luận sau)

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      79,
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      80 và Nhãn tuyên bố

      Các câu lệnh điều khiển vòng lặp sau được cung cấp (cùng cú pháp với Java/C/C++)

      • nghỉ. thoát khỏi vòng lặp trong cùng
      • tiếp tục. hủy bỏ lần lặp hiện tại và tiếp tục với lần lặp tiếp theo
      • nhãn. provides an identifier for a statement, which can be used by
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        81 and
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        82

      Cố gắng tránh những tuyên bố này, vì chúng khó theo dõi và duy trì

      Mảng

      Một mảng là một bộ sưu tập được lập chỉ mục. Một mảng có thể được sử dụng để lưu trữ danh sách các mục (phần tử) dưới một tên duy nhất với chỉ số nguyên đang chạy. Bạn có thể tham chiếu phần tử riêng lẻ thông qua chỉ số tích phân ở dạng

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      83. Hơn nữa, bạn có thể xử lý tất cả các phần tử của một mảng một cách thuận tiện thông qua một vòng lặp có chỉ số thay đổi

      Tạo một mảng thông qua "Trình khởi tạo mảng
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      84"

      Bạn có thể tạo một mảng bằng cách gán một ký tự mảng cho một biến, được gọi là Trình khởi tạo mảng, ở dạng

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      84. For examples,

      Hãy lưu ý rằng mảng chữ của JavaScript được đặt trong dấu ngoặc vuông

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      86, thay vì
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      87 trong Java/C/C++. JavaScript sử dụng
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      87 cho đối tượng theo nghĩa đen (sẽ được thảo luận sau)

      Bạn cũng có thể sử dụng Trình khởi tạo mảng để tạo một mảng có chỉ mục bị thiếu. For example,

      Accessing an Item

      Bạn có thể truy cập phần tử riêng lẻ của một mảng thông qua chỉ mục tích phân, ở dạng

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      83. Chỉ số của mảng bắt đầu từ 0 và phải là một số nguyên không âm

      Array's Property
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      90

      The length of the array is maintained in a variable (property) called

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      90, which can be accessed via
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      92. In fact, the property
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      93 returns the last integral index plus 1, as JavaScript's array index is 0-based. Nonetheless, you are allow to manipulate (write) the
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      93. For example,

      Dynamic Arrays

      Unlike Java/C/C++, the JavaScript array is dynamically allocated. You can add more elements to an array. You can also remove the content of an element using keyword

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      95. For examples,

      Accessing All Items using for-loop with index

      Array is usually processed collectively using a loop, e. g. ,

      The JavaScript's
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      77 loop

      JavaScript provides a special

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      97 loop to process all the elements in an array. The syntax is as follows, where
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      98 takes on the each of the index number of element which are not
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18

      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      8

      Ví dụ,

      The JavaScript's
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      29 loop

      JavaScript provides a special

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      01 loop to process all the elements in an array. The syntax is as follows, where
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      02 takes on the each of the element including the
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18

      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      9
      Add/Remove item(s)

      You can

      • Use index
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        04 to add one item to the end of an array
      • Use
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        95 to remove a particular index (set it to
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18)
      • Use
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        07 to add one or more items to the end of an array.
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        08 returns the resultant length of the array
      • Use
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        09 to add one or more items to the beginning of an array.
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        10 returns the resultant length of the array
      • Use
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        11 to remove and return the last item of an array
      • Use
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        12 to remove and return the first item of an array

      Ví dụ như,

      PurposeExampleAdd one item to the end of an array using index
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      13Add one or items to the end of an array using
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      07
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      08 returns the resultant length of the array. Add one or items to the beginning of an array using
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      09
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      10 returns the resultant length of the array. Remove and return the last item from an array using
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      11Remove and return the first item of an array using
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      12
      Array's Properties (Variables) and Operations (Functions)

      The

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      20 object has these commonly-used properties

      • length. the number of items including
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18 items. In fact,
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        93 is set to the last index plus 1

      It has these commonly-used methods

      • array. join([separator]). join the elements of an array together into a single string, separated by the
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        23 (defaulted to
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        24). For example,
      • str. split([separator, limit]). Reverse of
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        25. Take a string and split into an array based on the
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        26. For example,
      • array. concat(value1, value2, . , valueN). returns a new array composing of this array and the given arrays or values. For example,
      • array. reverse(). reverses the order of elements in the array, the first becomes last. For example,
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        0
      • array. sort(). sorts the elements in the array. For example, Take note take, by default, number are also sorted based on ASCII/Unicode order. To sort numbers numerically, you can supply a callback comparison function. The function shall take 2 arguments, say
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        27 and
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        28, and return a negative number if
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        29; a positive number if
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        30; and 0 if
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        31
      • array. slice(beginIdx, endIdx). extracts and returns a section of an array from
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        62 (inclusive) to
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        63 (exclusive). For example,
      • array. splice(startIdx, countToRemove, insertItem1, insertItem2, . ). removes elements from an array, and insert elements at its place. For example,
      • array. indexOf(searchItem[, startIdx]) and array. lastIndexOf(searchItem[, startIdx]). search for the index of the item forward or backward. It returns -1 if item cannot be found. For example,
      • push(). adds one or more elements to the end of an array and returns the resultant length of the array
      • pop(). removes and return the last element from an array
      • shift(). removes and returns the first element from an array
      • unshift(). adds one or more elements to the front of an array and returns the resultant length of the array

      Functional Programming in Filter-Map-Reduce pattern

      Arrays also support these iterative methods that iterate through each item of the array, to support functional programming of filter-map-reduce pattern

      • array. forEach(callback). takes a function with an argument which iterates through all the items in the array. NOTE. This syntax is meant to support functional programming of filter-map-reduce pattern
      • array. map(callback). return a new array, which contains all the return value from executing
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        34 on each item. For example,
      • filter(callback). return a new array, containing the items for which
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        34 returned
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26. For example,
      • array. every(callback). return
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 if
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        34 returns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 for ALL items
      • array. some(callback). return
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 if
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        34 returns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 for at least one item in the array

      For detailed specification and examples about a built-in object, check "Core JavaScript References"

      Associative Arrays of Key-Value Pairs

      An associative array is an array of key-value pair. Instead of using numbers 0, 1, 2,. as keys as in the regular array, you can use anything as key in an associative array. Associative arrays are used extensively in JavaScript and jQuery

      JavaScript does not support native associative array (it actually does not support native array too). In JavaScript, associative arrays (and arrays) are implemented as objects (to be elaborate later)

      You can create an associative array via the Object Initializer. For example,

      Take note that Array initializer uses square brackets

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      43; while object initializer (for associative array) uses curly brackets
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      44

      Functions

      Functions are useful

      • when you have to use the same codes several times
      • as the JavaScript event handler
      • make your program easier to read and understood

      A function accepts zero or more arguments from the caller, performs the operations defined in the body, and returns zero or a single result to the caller

      Các hàm là các đối tượng trong JavaScript (sẽ được thảo luận sau)

      The syntax for user-defined function is

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      1

      Functions are declared using the keyword

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      88. Unlike Java/C/C++, you do not have to specify the return-type and the types of the arguments because JavaScript is loosely typed. You can use a
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      46 statement to return a single piece of result to the caller anywhere inside the function body. If no
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      46 statement is used (or a
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      46 with no value), JavaScript returns
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18

      For client-side JavaScript, functions are generally defined in the

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      9 section, so that it is always loaded before being invoked

      To invoke a function

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      2
      Client-Side JavaScript Function Examples
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      3
      Function's
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      51

      Function has access to an additional built-in variable called

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      51 inside its body, which is an array containing all the arguments. For example,

      Pass by Value vs. Pass by Reference

      In JavaScript, primitive arguments are passed by value. That is, a copy of the variable is made and passed into the function. On the other hand, objects (including array and associative array) are passed by references. That is, if you modify the content of an object inside the function, the "copy" outside the function is also affected

      The Default Function Arguments

      JavaScript lets you invoke a function omitting some trailing arguments. It sets the omitted arguments to the value

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18. For example,

      You can use this feature to provide default value to function argument, for example,

      In the above example, we allow caller to omit the trailing arguments (

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      54,
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      55) or pass a
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21 value (which is a special literal for unallocated object)

      The common idiom in practice today is to use the short-circuited OR expression (as in

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      55) to provide default value if no value (
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18 or
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21) is passed, provided the valid inputs cannot be
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      49,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      88, and
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      91 that evaluate to
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27

      (ES6) Function with Default Parameter Values

      ES6 greatly simplify the above by introducing default parameter value in the form of

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      65. For example,

      (ES6) Rest/Spread Operator (
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      5) and Arrow-Function Notation

      ES6 also introduces rest/spread operator (

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      5) and arrow-function notation. See ES6 article

      Anonymous (Inline) Functions

      In JavaScript, you can define an anonymous function (without a function name) using the following syntax

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      4

      Anonymous function is often used in event handlers and others

      Function Variables

      In JavaScript, a variable can hold a primitive (number, string, boolean) or an object

      In JavaScript, functions are first-class object. Hence, a variable can also hold a function object. For example,

      Two Ways in defining a function

      As seen in the above example, there are two ways to define a function

      1. Use a function declaration statement in the form of.
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        5
      2. Use a function expression by assigning an anonymous function to a variable.
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        6
      Functions as Arguments

      Recall that a function takes zero or more arguments from the caller. In JavaScript, arguments can be a primitive or object. Since functions are first-class objects in JavaScript, a function may take another function as its argument

      For example, the following function take a function and an array as its arguments, and apply the function to the array

      Nested Functions

      In JavaScript, you can define a function inside a function. For example,

      Function as the return value

      You can return a function from a function. For example,

      Interacting with Users

      Client-side JavaScript provides these built-in top-level functions for interacting with the user

      • alert(str). Pop-up a box to alert user for important information. The user will have to click "OK" to proceed. The
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        8 returns nothing (or
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18)
      • prompt(str, defaultValue). Pop-up a box to prompt user for input, with an optional
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        70. The
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        15 returns the user's input as a string. For example,
      • xác nhận(str). Pop-up a box and ask user to confirm some information. The user will have to click on "OK" or "Cancel" to proceed. The
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        16 which returns a boolean value. For example,
      • document. write(str), document. writeln(str). Write the specified string to the current document. The
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        57 (write-line) writes a newline after the string, while
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        85 does not. Take note that browsers ignores extra white spaces, including newlines, in an HTML document, and treat newline as a single blank character. You need to write a
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        60 or
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        59 tag to ask the browser to display a line break

      The following top-level built-in functions are available to client-side as well as server-side (or standalone)

      • console. log(value). write to the system (or error) console, used mainly for debugging

      Other Top-level Built-in Functions

      JavaScript also pre-defines the following top-level global functions

      • parseInt(str), parseFloat(str). parses the given
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        3 and returns the numeric value or
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        91 (Not-A-Number). The
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        79 accepts an optional
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        80 (or base). For example,
      • Number(object). returns the number representation of the object. It works for
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        81 object, as well as many objects such as
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        43
      • isFinite(number). returns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 if
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        74 is not
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        91,
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        86 or
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        87
      • isNaN(number). returns
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 if number is
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        91. Useful for checking the output of
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        77 and
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        29
      • eval(codes). evaluate the given JavaScript codes, which could be an expression or a sequence of statements. For example. [TODO]
      • encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent(). encode or decode name-value pairs for the HTTP request, by replacing special characters with
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        92. For example. [TODO]

      An Introduction to Events (for Client-side JavaScript)

      Client-side JavaScript are often event-driven. That is, a function (called event handler) will be fired in response to a certain user's or browser's action that generates an event

      The commonly-used events are

      • 
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        93. generated when the user clicks on an HTML element
      • 
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        94,
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        95. generated when the user positions the mouse pointer inside/away from the HTML element
      • alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        03,
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        97. generated after the browser loaded a document, and before the next document is loaded, respectively

      The event handler, called

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      98 (such as
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      82,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      00,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      94), is the function that responses to an event. The event handler is typically attached to the target HTML tag, e. g. ,

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      7

      More about events in the later sections

      Objects

      Objects are Everywhere

      JavaScript is object-oriented (OO), and objects are everywhere in JavaScript. Arrays are objects. Functions are also objects. Primitives like string and number are auto-box to objects in many operations. Almost everything in JavaScript can be an object, possibly except the

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18 and
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21 (unallocated object)

      It is crucial to understanding objects to be proficient in JavaScript, as they are everywhere, and they are used extensively in JavaScript plug-ins like jQuery, AngularJS, React, and etc

      JavaScript, however, does not support all the OO features, so as to keep the language simple

      Properties and Methods

      In conventional object-oriented programming languages (like Java/C++/C#), an object is a collection of properties and methods under a common name

      • Properties (also called variables, attributes). contains values associated with the object
      • Methods (also called operations, functions). contains actions that the object can perform

      For example, a

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      04 object has properties such as
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      05,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      06,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      07,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      08; and methods such as
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      09,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      10,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      11,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      12

      Unlike full-fledged OO languages, JavaScript is designed on a simple OO paradigm. A JavaScript object is simply a collection of properties. A property is an association between a key (or name) and value (i. e. , key-value pair or name-value pair). If the value is a function (recall that JavaScript functions are first-class objects that can be assigned to variables), the property is known as a method. Nói cách khác, JavaScript không phân biệt giữa thuộc tính và phương thức như các ngôn ngữ hướng đối tượng thông thường. A method is simply a property holding a function

      Creating Objects via Object Initializer with Object Literal

      You can create a new object by directly assign an object literal to a variable, known as Object Initializer. The syntax is as follows

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      8
      • A JavaScript object is a collection of properties. Each property is an association of a name (key) and value pair
      • The name and value are separated by a colon
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        13
      • The name-value pairs are separated by commas
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        24. No comma is needed after the last name-value pair
      • The property name could be a string or a number. For string name which is NOT a valid identifier (an valid identifier begins with an alphabet, underscore or dollar sign; and does not containing special characters like blank or
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        15), you need to enclose the name with single or double quotes. Otherwise, the quotes are optional. For example, Take note that if the property name's string is a proper identifier, you can omit the quotes. Điều này thường được thực hiện bởi các nhà phát triển
      • Giá trị thuộc tính có thể là một chữ, một biến hoặc một biểu thức
      • Giá trị của một thuộc tính có thể lấy tất cả các loại JavaScript, bao gồm cả các nguyên hàm (chuỗi, số, boolean), đối tượng hoặc hàm. Thuộc tính được gọi là một phương thức, nếu nó chứa một chức năng. JavaScript không phân biệt giữa các biến và phương thức như các ngôn ngữ OO thông thường (Java/C++/C#). Một phương thức chỉ đơn giản là một thuộc tính nắm giữ một chức năng
      • Các đối tượng được tạo thông qua Trình khởi tạo đối tượng là các thể hiện của lớp
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        16
      Dấu phẩy ở cuối

      Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. Nếu bạn muốn thêm thuộc tính mới, bạn có thể thêm một dòng mới mà không sửa đổi dòng cuối cùng trước đó nếu dòng đó đã sử dụng dấu phẩy ở cuối. Điều này làm cho các khác biệt kiểm soát phiên bản sạch hơn và việc chỉnh sửa mã có thể ít rắc rối hơn. Ngay từ đầu, JavaScript đã cho phép sử dụng dấu phẩy ở cuối mảng và sau đó đã thêm chúng vào đối tượng chữ (ES?), và gần đây hơn, vào các tham số chức năng (ES?)

      Người truy cập tài sản. Toán tử Dấu chấm và Chỉ mục (Dấu ngoặc vuông)

      Trong JavaScript, bạn có thể truy cập các thuộc tính của đối tượng thông qua một trong hai

      1. Toán tử dấu chấm (
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        17). in the form of
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        18 or
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        19, just like Java/C++
      2. Toán tử chỉ số
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        65 (hoặc ký hiệu ngoặc đơn). Trong JavaScript, bạn cũng có thể sử dụng toán tử chỉ mục để truy cập các thuộc tính của đối tượng, ở dạng
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        21, giống như truy cập một mục mảng
      Toán tử
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      95

      Bạn có thể xóa một thuộc tính (bao gồm cả phương thức) thông qua "

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      23"

      Người điều hành
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      24

      Bạn có thể sử dụng toán tử

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      24 để kiểm tra xem một thể hiện cụ thể có thuộc về một lớp đối tượng cụ thể hay không. g. ,

      The
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      26 method

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      27 trả về
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 nếu
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      29 có giá trị cụ thể là
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      30

      Người điều hành
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      31

      Bạn có thể sử dụng toán tử

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      31 để kiểm tra xem một thuộc tính cụ thể có thuộc về một đối tượng hay không, ví dụ:

      ví dụ 1. Một đối tượng đơn giản, xóa và thêm thuộc tính

      Không giống như Java/C++, bạn có thể xóa hoặc thêm các thuộc tính mới vào một đối tượng JavaScript hiện có

      ví dụ 2. Một đối tượng có thuộc tính đối tượng

      Thuộc tính của một đối tượng có thể chứa các nguyên hàm (số, chuỗi). Nó cũng có thể giữ một đối tượng

      ví dụ 3. Một đối tượng có chức năng

      Thuộc tính của đối tượng JavaScript cũng có thể chứa một hàm (hoặc phương thức). Bạn có thể gán cho một hàm ẩn danh hoặc một hàm được đặt tên như trong ví dụ sau

      Trích dẫn hay không trích dẫn tên trong cặp tên-giá trị?

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      33 (của cặp
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      34) có thể lấy một chuỗi hoặc một số, e. g. ,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      35

      Many developers do not enquote the string

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      33, if it is a valid identifier (a valid identifier begins with an alphabet,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      37 or
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      38; and does not contain special characters like blank and
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      15). Ví dụ:
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      40 giống như
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      41. Tuy nhiên, bạn cần trích dẫn một chuỗi
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      33 nếu nó KHÔNG phải là một mã định danh hợp lệ, e. g. ,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      43

      Chuỗi

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      95 phải được trích dẫn;

      Toán tử dấu chấm vs. Toán tử chỉ mục (Dấu ngoặc vuông)

      Bạn có thể truy cập thuộc tính của một đối tượng thông qua toán tử dấu chấm hoặc toán tử chỉ mục. Toán tử dấu chấm thường được sử dụng vì nó ngắn gọn hơn và được sử dụng trong các ngôn ngữ hướng đối tượng thông thường. Nhưng toán tử chỉ mục cho phép truy cập vào các thuộc tính chứa các ký tự đặc biệt và lựa chọn các thuộc tính bằng cách sử dụng các biến, điều này không khả thi với toán tử dấu chấm. Ví dụ,

      Lặp lại tất cả các thuộc tính của một đối tượng

      Có ba cách để lặp qua tất cả các thuộc tính của một đối tượng

      1. vì. Trong. đi qua tất cả các thuộc tính có thể đếm được của một đối tượng và chuỗi nguyên mẫu của nó
      2. Mục tiêu. phím (obj). trả về một mảng gồm tất cả các tên thuộc tính có thể đếm được
      3. Mục tiêu. getOwnPropertyNames(obj). trả về một mảng của tất cả các tên thuộc tính

      Ví dụ,

      Operator
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      45

      Thiết lập đối tượng mặc định cho một tập hợp các câu lệnh. Cú pháp là

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      9
      Giá trị theo nghĩa đen của
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21 so với
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18

      Như đã thảo luận trước đó

      • Một biến không được khai báo có loại
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18 và giá trị của nó không thể được tham chiếu
      • A declared but uninitialized variable has type of
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18, with a special literal value of
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        18 too. [______76_______18 vừa là giá trị kiểu vừa là giá trị theo nghĩa đen. ]
      • Một đối tượng chưa phân bổ có giá trị theo nghĩa đen đặc biệt là
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21. Hãy lưu ý rằng
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21 là một đối tượng

      Trong các biểu thức boolean, cả

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21 và
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18 đều có giá trị là
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27 (cùng với chuỗi rỗng
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      13, số
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      49 và
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      91, và boolean
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27). Để kiểm tra giá trị
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21, hãy sử dụng
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      62, vì
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      63 (hoặc
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      93) trả về
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 cho
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      21,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      18,
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      13,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      49,
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      91 và
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      27

      Các đối tượng JavaScript tích hợp

      JavaScript cung cấp nhiều đối tượng toàn cục hữu ích, chẳng hạn như

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      20,
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      43,
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      81 và
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      89

      Tôi sẽ thảo luận về một số đối tượng thường được sử dụng ở đây. Để biết thông số kỹ thuật chi tiết và các ví dụ về một đối tượng tích hợp sẵn, hãy kiểm tra "Tham khảo JavaScript chính"

      Tạo một đối tượng thông qua Constructor và Toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47

      Tất cả các đối tượng dựng sẵn định nghĩa một cái gọi là hàm tạo, là một phương thức đặc biệt có cùng tên với đối tượng, để tạo các thể hiện mới. Để gọi hàm tạo, bạn cần sử dụng toán tử đặc biệt

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47

      Ví dụ: đối tượng

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      43 có một phương thức khởi tạo có tên là
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      79. Để xây dựng một thể hiện mới của đối tượng
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      43

      Toán tử
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      31 và
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      24

      Như đã thấy từ các ví dụ trên, toán tử

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      31 trả về loại giá trị, có thể là loại nguyên thủy (chẳng hạn như số, chuỗi, boolean) hoặc đối tượng (bao gồm cả hàm)

      Toán tử

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      24 kiểm tra xem giá trị có phải là một thể hiện của một lớp không

      Phương pháp
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      43 và
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      86

      Các phương thức sau có sẵn cho tất cả các đối tượng JavaScript tích hợp

      • toString(). trả về một chuỗi mô tả về đối tượng này
      • giá trị của(). chuyển đổi đối tượng này thành một giá trị nguyên thủy, chẳng hạn như
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        74

      Mảng kết hợp và đối tượng
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      20

      For an introduction to arrays, read the "" section

      Ngày nay chúng ta thường tạo mảng thông qua Array Initializer dưới dạng

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      84. Tuy nhiên, vì mảng là đối tượng của lớp
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      20, nên chúng ta cũng có thể xây dựng một mảng bằng cách gọi hàm tạo
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      91 với toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47. Điều này phức tạp hơn và do đó ngày nay ít được sử dụng hơn (nhưng có thể xuất hiện trong các mã và sách cũ)

      Ví dụ,

      Mảng kết hợp là một "Đối tượng" chung, không phải là Đối tượng "Mảng"

      For an introduction to arrays, read the "" section

      JavaScript không thực sự hỗ trợ mảng kết hợp, nhưng cho phép bạn thêm các thuộc tính vào một đối tượng, sử dụng cú pháp mảng kết hợp và toán tử chỉ mục. Nói cách khác,

      Sử dụng một đối tượng mảng cho mảng kết hợp

      Trong JavaScript, chúng ta thường tạo một mảng kết hợp bằng cách sử dụng một đối tượng chung (thông qua Trình khởi tạo đối tượng). Bạn cũng có thể tạo một mảng kết hợp thông qua hàm tạo

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      20. Ví dụ như,

      In this case, additional properties are added into an

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      20 object as object's properties. The array has
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      93 of 0, as
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      93 reflects only the numerical indexes. Using an array object for associative array is not recommended. Tôi mô tả ở đây cho đầy đủ

      The
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      81 Object vs
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 Primitive Type

      JavaScript cung cấp loại

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 nguyên thủy. Nó cũng cung cấp một lớp
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      81 như một lớp bao bọc cho nguyên hàm
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30. Read "" for primitive
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30

      Một chuỗi có thể được tạo theo hai cách

      1. gán trực tiếp một chuỗi ký tự ở dạng
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        03 hoặc
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        04 cho một biến, dẫn đến một nguyên hàm
        
        
        
        
          
          JavaScript Example: Variables and functions prompt() and confirm()
          
        
        
          

        Welcome to JavaScript!

        30
      2. invoke the
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        81 object constructor to construct a
        
        
        
        
          
          JavaScript Example: Loop
        
        
          

        Testing Loop

        81 object

      Ví dụ như,

      The former is a primitive

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30, while the latter is a
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      81 object

      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      81 object is a wrapper for primitive string, which provides many useful methods for manipulating strings. Thấy ""

      A

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      30 primitive is "auto-box" to a
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      81 object, when you invoke a
      
      
      
      
        
        JavaScript Example: Loop
      
      
        

      Testing Loop

      81 object's methods. Ví dụ,

      The
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      89 Object vs
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 Primitive Type

      JavaScript cung cấp loại

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 nguyên thủy. Nó cũng cung cấp một lớp
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      89 như một lớp bao bọc cho lớp nguyên thủy
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74. Đọc "" cho nguyên thủy
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74

      Đối tượng

      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      89 là đối tượng bao bọc cho
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 nguyên thủy, cung cấp nhiều thuộc tính và phương thức. Một lần nữa, một nguyên hàm
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      74 là hộp tự động cho đối tượng
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      89, khi bạn gọi hoạt động của đối tượng
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      89. Thấy ""

      Đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      25 so với kiểu boolean
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      26

      JavaScript provides primitive

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 type. Nó cũng cung cấp một lớp
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      25 như một lớp bao bọc cho nguyên thủy
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78. Read "" for primitive
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78

      Đối tượng

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      25 là một đối tượng bao bọc cho
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      78 nguyên thủy, cung cấp nhiều thuộc tính và phương thức

      Đối tượng
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      43

      Các hàm tạo thường dùng

      • Ngày mới(). xây dựng một đối tượng
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        43 với ngày giờ hiện tại
      • Ngày mới (dateTimeStr). constructs a
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        43 object with the given date-time string in an acceptable form (e. g. , "Tháng, ngày, năm, giờ. phút. seconds")
      • ngày mới (năm, tháng, ngày). where month is 0-11 for Jan to Dec
      • new Date(year, Month, day, hours, minutes, seconds, milliseconds)

      Commonly-used methods

      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        62, setDate(), getMonth(), setMonth(), getFullYear(), setFullYear(). get/set the date (1-31), month (0-11 for Jan to Dec), year (4-digit year)
      • getDay(). get the day of the week (0-6 for Sunday to Saturday)
      • getHours(), setHours(), getMinutes(), setMinutes(), getSeconds(),
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        37. get/set the hours/minutes/seconds
      • getTime(), setTime(). get/set the number of milliseconds since January 1, 1970, 00. 00. 00

      Đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      38

      Thuộc tính thường dùng

      • E, PI. Hằng số Euler và
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        39
      • LN2, LN10, LOG2E, LOG10E. ln(2), ln(10), log2(e), log10(e)
      • SQRT2, SQRT1_2. căn bậc hai của 2 rưỡi

      Commonly-used methods

      • cơ bụng(x)
      • sin(a), cos(a), tan(a), asin(x), acos(x), atan(x), atan2(x,y)
      • ô(x), sàn(x), tròn(x)
      • exp(x), log(x), pow(base,exp), sqrt(x)
      • tối đa(x,y), tối thiểu(x,y)
      • ngẫu nhiên(). trả về một số giả ngẫu nhiên giữa 0 và 1

      Đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40

      Để biết giới thiệu về các chức năng, hãy đọc phần ""

      Mỗi chức năng trong JavaScript thực sự là một đối tượng

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40

      Hàm biến

      Một biến có thể được gán một đối tượng

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40 (được gọi là biến hàm), nhận một loại đối tượng đặc biệt có tên là
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      88. [Trong JavaScript, loại đối tượng là
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      88 (đối với đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40) hoặc
      
      
      
      
        
        JavaScript Example: Variables and functions prompt() and confirm()
        
      
      
        

      Welcome to JavaScript!

      20 (đối với bất kỳ đối tượng nào không phải là
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40). ]

      Ví dụ,

      Chức năng ẩn danh

      Nếu một hàm chỉ được gọi một lần, bạn có thể sử dụng hàm ẩn danh, bỏ qua tên hàm. Các hàm ẩn danh thường được sử dụng làm trình xử lý sự kiện, e. g. ,

      Bạn có thể định nghĩa một hàm nội tuyến (ẩn danh) và gán nó cho một biến như sau

      Trình xây dựng hàm

      Bên cạnh việc sử dụng từ khóa

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      88, bạn cũng có thể sử dụng hàm tạo
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40 (với toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47) để xác định đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40. Điều này không dễ hiểu và do đó, không được khuyến nghị

      Cú pháp của hàm tạo

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      40 là

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      0

      Đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      53

      Định dạng đầu ra bảng điều khiển

      bảng điều khiển. log('formattingStr', arg1, arg2,. ). Bạn có thể sử dụng cú pháp giống như

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      14 của C với các chỉ định định dạng (chẳng hạn như
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      55,
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      56,
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      57) để định dạng chuỗi đầu ra

      Bạn có thể sử dụng %c để áp dụng các quy tắc CSS, e. g. ,

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      1
      Logging Methods

      JavaScript hỗ trợ 5 cấp độ ghi nhật ký, theo thứ tự mức độ nghiêm trọng

      1. bảng điều khiển. lỗi()
      2. bảng điều khiển. cảnh báo()
      3. bảng điều khiển. nhật ký()
      4. bảng điều khiển. thông tin()
      5. bảng điều khiển. gỡ lỗi()
      Các phương pháp gỡ lỗi khác
      • bảng điều khiển. dấu vết(). in dấu vết ngăn xếp hiện tại
      • bảng điều khiển. dir(). hiển thị danh sách tương tác các thuộc tính của đối tượng JavaScript đã chỉ định
      • bảng điều khiển. dirxml(). hiển thị trong XML các thuộc tính của đối tượng JavaScript được chỉ định
      • bảng điều khiển. khẳng định(). viết một thông báo lỗi vào bảng điều khiển nếu khẳng định là sai;
      Đo lường thời gian
      • bảng điều khiển. thời gian([nhãn]). bắt đầu một bộ đếm thời gian mới
      • bảng điều khiển. timeEnd([nhãn]). kết thúc bộ hẹn giờ và trả về thời gian tính bằng mili giây kể từ khi bộ hẹn giờ bắt đầu
      • bảng điều khiển. timeLog([nhãn]). trả về thời gian đã trôi qua kể từ khi bộ đếm thời gian bắt đầu

      Tìm hiểu thêm về đối tượng

      dựa trên nguyên mẫu so với. OO dựa trên lớp

      OO của JavaScript dựa trên nguyên mẫu, thay vì dựa trên lớp như Java/C++/C#

      Ngôn ngữ OO dựa trên lớp (chẳng hạn như Java/C++/C#) được thành lập dựa trên các khái niệm về lớp và thể hiện. Một lớp là một bản in màu xanh hoặc khuôn mẫu của những thứ cùng loại. Một thể hiện là một hiện thực cụ thể của một lớp. Ví dụ: "

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      58" là một lớp; . Trong ngôn ngữ OO dựa trên lớp, trước tiên bạn phải viết định nghĩa lớp, trước khi bạn có thể tạo các thể hiện dựa trên định nghĩa lớp. Các thể hiện được tạo có các thuộc tính và phương thức giống hệt như lớp - không hơn, không kém

      Mặt khác, một đối tượng (hoặc phiên bản) băm đơn giản dựa trên ngôn ngữ OOP nguyên mẫu (chẳng hạn như JavaScript). Một đối tượng mới có thể được xây dựng dựa trên một đối tượng hiện có dưới dạng nguyên mẫu. Không có định nghĩa lớp và do đó, chúng còn được gọi là ngôn ngữ OO không có lớp

      trong JavaScript

      • Mỗi đối tượng JavaScript có một nguyên mẫu. Nguyên mẫu cũng là một đối tượng. Một đối tượng JavaScript được tạo từ một đối tượng nguyên mẫu và nhận các thuộc tính ban đầu của nó từ nguyên mẫu
      • Mỗi đối tượng JavaScript có một thuộc tính bên trong được gọi là
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        62, thuộc tính này chứa đối tượng nguyên mẫu của nó. Khi bạn xác định một đối tượng mới,
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        62 của nó được đặt thành một đối tượng tích hợp có tên là
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        64. Nói cách khác, đối tượng mới sử dụng
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        64 làm nguyên mẫu và nhận các thuộc tính ban đầu của nó từ
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        64. Thuộc tính
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        62 không thể đếm được, tôi. e. , nó sẽ không hiển thị trong vòng lặp
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        77
      Thí dụ. Object Prototype

      ghi chú

      • Chuỗi nguyên mẫu. Trong ví dụ trên, nguyên mẫu của
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        69 là
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        64, nguyên mẫu của nó là
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        21. Điều này tạo thành một chuỗi nguyên mẫu của
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        72.
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        69 định nghĩa hai thuộc tính riêng.
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        74 và
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        75. Khi bạn gọi
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        76, không thể tìm thấy trong
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        69, nhưng được tìm thấy trong nguyên mẫu của nó
      • Mục tiêu. getPrototypeOf(obj). Thay vì sử dụng thuộc tính
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        78 được sử dụng trong ví dụ trên, vốn KHÔNG có trong đặc tả chính thức của JavaScript, bạn nên sử dụng phương thức
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        79 để lấy nguyên mẫu của một đối tượng

      Xác định các đối tượng tùy chỉnh của riêng bạn thông qua một Constructor

      Trong JavaScript, bạn có thể tạo các đối tượng tùy chỉnh của riêng mình bằng cách xác định hàm tạo. Hàm tạo chỉ đơn giản là một hàm, có thể được gọi thông qua toán tử

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47 để tạo và khởi tạo các đối tượng mới. Nhớ lại rằng JavaScript dựa trên nguyên mẫu và không có lớp. Một đối tượng mới được tạo từ một đối tượng nguyên mẫu và nhận các thuộc tính ban đầu của nó từ đối tượng nguyên mẫu này. Hàm khởi tạo cung cấp nguyên mẫu để tạo và khởi tạo các phiên bản mới (sẽ được giải thích sau)

      Theo quy ước, tên hàm tạo bắt đầu bằng một chữ cái viết hoa

      Từ khóa
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      09

      "______59_______09" đề cập đến đối tượng hiện tại.

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      83 đề cập đến tài sản của đối tượng này

      Thí dụ. Hàm tạo đối tượng

      Hãy để chúng tôi định nghĩa một hàm tạo cho các đối tượng

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      84 tùy chỉnh của chúng tôi, với các thuộc tính
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      74,
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      86,
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      87 và
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      88

      Làm thế nào nó hoạt động?
      1. Các thuộc tính (bao gồm các phương thức) được khai báo bên trong hàm tạo thông qua
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        83, sau đó có thể được khởi tạo
      2. Các phương thức có thể được khai báo bên trong hàm tạo thông qua
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        90. Có một số cách để cung cấp định nghĩa của phương thức
        1. Xác định một
          My First JavaScript says
          

          Hello world, again!

          This document was last modified on mm/dd/yyyy hh:mm:ss.

          88 bình thường, và gán
          
          
          
          
            
            JavaScript Example: User-defined function and onclick Event Handler
            
          
          
            

          Example on event and user-defined function

          92 (không có dấu ngoặc đơn, là một loại
          My First JavaScript says
          

          Hello world, again!

          This document was last modified on mm/dd/yyyy hh:mm:ss.

          88) cho
          
          
          
          
            
            JavaScript Example: User-defined function and onclick Event Handler
            
          
          
            

          Example on event and user-defined function

          90, như trong trường hợp
          
          
          
          
            
            JavaScript Example: User-defined function and onclick Event Handler
            
          
          
            

          Example on event and user-defined function

          88
        2. Gán
          
          
          
          
            
            JavaScript Example: User-defined function and onclick Event Handler
            
          
          
            

          Example on event and user-defined function

          90 cho một hàm nội tuyến, như trong
          
          
          
          
            
            JavaScript Example: User-defined function and onclick Event Handler
            
          
          
            

          Example on event and user-defined function

          87
        3. Gọi hàm tạo
          
          
          
          
            
            JavaScript Example: User-defined function and onclick Event Handler
            
          
          
            

          Example on event and user-defined function

          40 với toán tử
          My First JavaScript says
          

          Hello world, again!

          This document was last modified on mm/dd/yyyy hh:mm:ss.

          47 (hiếm khi sử dụng).
          
          
          
          
            
            JavaScript Example: The Date object
            
          
          
          
          2
      3. Để gọi một phương thức, hãy sử dụng
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        00
      4. 
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        43 là một phương thức đặc biệt, trả về một chuỗi mô tả về đối tượng này.
        
        
        
        
          
          JavaScript Example: The Date object
          
        
        
        
        43 sẽ được gọi ngầm nếu một đối tượng được truyền vào toán tử
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        9,
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        56 hoặc
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        00 (như Java)

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62 và
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      07

      Xem lại toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47

      Trong JavaScript, lệnh gọi hàm tạo mới (args) thực hiện như sau

      1. Tạo một đối tượng chung mới bằng cách sử dụng gốc tích hợp sẵn
        for ( initialization ; test ; post-processing ) {
           body ;
        }
        16 làm nguyên mẫu
      2. Gán đối tượng mới tạo này cho biến
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        09
      3. Chạy hàm tạo, thường thêm các thuộc tính vào
        alert('hello, world')
        document.write('hello world, again')
        console.log('hello world, again and again')
        09
      4. Đặt thuộc tính bên trong
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        62 thành
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        13 (trong ví dụ trên,
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        14). Thuộc tính
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        62 không thể đếm được (i. e. , nó sẽ không xuất hiện trong vòng lặp
        if ( condition-1 ) {
           block-1 ;
        } else if ( condition-2 ) {
           block-2 ;
        } else if ( condition-3 ) {
           block-3 ;
        ......
        ......
        } else {
           block-n ;
        }
        77). Nó được sử dụng để duy trì cái gọi là "chuỗi nguyên mẫu" của các đối tượng
      5. Trả về đối tượng vừa tạo

      Hãy lưu ý rằng bạn có thể gọi hàm tạo mà KHÔNG CÓ toán tử

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47, e. g. ,
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      18. Trong trường hợp này, hàm tạo sẽ được chạy theo chức năng thông thường. Sẽ không có bất kỳ sự liên kết nào của các thuộc tính
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      09 và
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62;

      Hãy thử theo dõi đối tượng được tạo trong ví dụ trên thông qua Công cụ dành cho nhà phát triển của Chrome, công cụ này có thể hiển thị thuộc tính nội bộ

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62

      Một vi dụ khac
      Tài sản
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      07 của nhà xây dựng

      Tất cả các đối tượng hàm tạo JavaScript (thực tế là tất cả các đối tượng hàm) đều có một thuộc tính đặc biệt gọi là

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      07. Theo mặc định, nó chứa một đối tượng có thể được sử dụng làm nguyên mẫu để tạo đối tượng mới

      Trong ví dụ về

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      84, hàm khởi tạo
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      84 có một
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      14, giữ một đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      84 được sử dụng làm đối tượng nguyên mẫu. Khi một đối tượng
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      84 mới được tạo thông qua toán tử
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      47, thuộc tính
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62 của đối tượng mới được tạo được đặt thành
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      14

      Thêm các thuộc tính được chia sẻ vào thuộc tính
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      07 của Constructor

      Hàm tạo_______36_______07 được sử dụng để triển khai các thuộc tính dùng chung (thường là các phương thức dùng chung) cho tất cả các đối tượng mà nó tạo nguyên mẫu. Trong JavaScript, bạn có thể tự động thêm các thuộc tính trong thời gian chạy vào

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      07. Các thuộc tính/phương thức được thêm vào sẽ có sẵn cho tất cả các đối tượng (đối tượng mới cũng như hiện có). Ví dụ,

      Khi bạn tham chiếu một thuộc tính, trước tiên, JavaScript sẽ kiểm tra xem thuộc tính đó có tồn tại cục bộ (hoặc thuộc tính sở hữu không); . Nhớ lại rằng cuộc gọi

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      36 đặt
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62 thành
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      13. Do đó, các thuộc tính được thêm vào
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      13 có sẵn cho (và được chia sẻ bởi) tất cả các đối tượng được tạo nguyên mẫu bởi hàm tạo này

      Trong thực tế, chúng tôi định nghĩa các phương thức (và các biến tĩnh) trong

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      13 được chia sẻ bởi tất cả các đối tượng;

      Tra cứu chuỗi nguyên mẫu

      Các đối tượng ở dạng chuỗi nguyên mẫu JavaScript. Trong ví dụ trên, một chuỗi nguyên mẫu là

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      41

      Khi bạn tham chiếu một thuộc tính đối tượng, JavaScript sẽ làm theo các bước sau để định vị thuộc tính

      1. Kiểm tra xem thuộc tính có tồn tại cục bộ không (i. e. , tài sản riêng)
      2. Nếu không, hãy kiểm tra thuộc tính
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        62. Điều này tiếp tục đệ quy lên chuỗi nguyên mẫu. Quá trình này được gọi là "tra cứu trong chuỗi nguyên mẫu"

      Do đó, trong ví dụ trên, tất cả các thuộc tính trong

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      14 và
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      64 được kế thừa bởi
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      45

      for ( initialization ; test ; post-processing ) {
         body ;
      }
      24 Toán tử

      Mọi đối tượng đều có thuộc tính đối tượng

      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62 (ngoại trừ
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      16); . Vì vậy, các đối tượng có thể được liên kết bằng 'kế thừa nguyên mẫu' với các đối tượng khác. Bạn có thể kiểm tra tính kế thừa bằng cách so sánh
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62 của một đối tượng với
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      07 của hàm tạo. JavaScript cung cấp lối tắt. toán tử
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      24 kiểm tra một đối tượng dựa trên một hàm và trả về
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      26 nếu đối tượng kế thừa từ nguyên mẫu hàm. Ví dụ,

      [LÀM]

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      54

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      54 cho phép bạn tạo một đối tượng mới dựa trên đối tượng nguyên mẫu đã cho. Nó chỉ đơn giản là đặt
      
      
      
      
        
        JavaScript Example: User-defined function and onclick Event Handler
        
      
      
        

      Example on event and user-defined function

      62 cho đối tượng nguyên mẫu đã cho, để tất cả các thuộc tính của nguyên mẫu có sẵn cho đối tượng mới. Sử dụng
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      54, Bạn KHÔNG cần xác định hàm tạo và chạy
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      58 để tạo đối tượng mới

      Kế thừa thông qua chuỗi nguyên mẫu

      Bạn có thể triển khai kế thừa trong JavaScript thông qua chuỗi nguyên mẫu. Ví dụ,

      Làm thế nào nó hoạt động

      [LÀM]

      Getter và Setter

      Trong các ngôn ngữ như Java, bạn có thể khai báo một biến là

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      59 và định nghĩa trình getter/setter
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      60 để truy cập biến
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      59. JavaScript không thực sự hỗ trợ truy cập
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      59?. Nhưng bạn cũng có thể định nghĩa các phương thức thông thường
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      63 hoặc
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      64 là getter/setter như Java

      Ngoài ra, trong JavaScript, bạn có thể định nghĩa getter/setter là thuộc tính thay vì phương thức theo một trong hai cách sau

      1. thông qua các chức năng tích hợp sẵn
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        65 hoặc
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        66;
      2. bên trong "trình khởi tạo đối tượng" thông qua các từ khóa
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        67 và
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        68
      ví dụ 1. Xác định Getters/Setters qua
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      65 hoặc
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      66

      ghi chú

      1. 
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        71 cho phép bạn xác định nhiều thuộc tính;
      2. Bạn gọi các thuộc tính getters/setters giống như toán tử gán, thay vì thông qua lệnh gọi hàm
      ví dụ 2. Xác định Getter/Setter bên trong Trình khởi tạo đối tượng

      ghi chú

      1. getter/setter được xác định bên trong Trình khởi tạo đối tượng thông qua các từ khóa
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        67 và
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        68 với một cú pháp khá lạ, như trong ví dụ

      Thuộc tính của
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      16

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      76

      Tất cả các đối tượng JavaScript được thừa hưởng một thuộc tính đặc biệt có tên là

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      77, thuộc tính này chứa tham chiếu đến hàm đã tạo đối tượng. Ví dụ,

      [TODO] thêm thuộc tính của đối tượng

      API mô hình đối tượng tài liệu (DOM) cho JavaScript

      Mô hình đối tượng tài liệu (DOM), là một API tiêu chuẩn cho phép các lập trình viên truy cập và thao tác nội dung của tài liệu HTML/XHTML/XML một cách linh hoạt bên trong chương trình của họ. Nó mô hình hóa tài liệu HTML/XHTML/XML dưới dạng cấu trúc dạng cây, hướng đối tượng, được gọi là cây DOM, bao gồm các nút giống như các thẻ (phần tử) và nội dung. DOM cũng cung cấp giao diện để xử lý sự kiện, cho phép bạn phản hồi hành động của người dùng hoặc trình duyệt

      API DOM được triển khai bằng nhiều ngôn ngữ như Java, JavaScript, Perl và ActiveX. Đặc tả DOM API được duy trì bởi W3C. DOM có nhiều cấp độ khác nhau

      • DOM Cấp 0 (DOM0) (Trước W3C). lỗi thời
      • DOM Cấp 1 (DOM1) (W3C tháng 10 năm 1998). lỗi thời
      • DOM Cấp 2 (DOM2) (W3C tháng 11 năm 2000) và DOM Cấp 2 HTML (HTML DOM2) (W3C tháng 1 năm 2003)
      • DOM Cấp 3 (DOM3) (W3C tháng 4 năm 2004). chưa được hỗ trợ đầy đủ bởi các trình duyệt

      jQuery tốt hơn nhiều trong việc chọn và thao tác phần tử DOM. Bạn nên sử dụng jQuery trong sản xuất. Tôi giữ những phần này ở đây cho đầy đủ

      Tìm và chọn phần tử

      Trong JavaScript, chúng tôi thường sử dụng API DOM để chọn các thành phần trong tài liệu hiện tại, để truy cập hoặc thao tác nội dung của chúng. Các chức năng được sử dụng phổ biến nhất là

      HàmMô tảVí dụ_______36_______78Trả về phần tử với giá trị duy nhất đã cho
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      19.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      3_______36_______80Trả về một mảng các phần tử với tên thẻ đã cho.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      4_______36_______81Trả về một mảng các phần tử với tên thuộc tính
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      82 đã cho.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      5
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      83Trả về một mảng các phần tử với thuộc tính
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      33 đã cho.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      6

      Bạn có thể sử dụng ký tự đại diện

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      85 trong
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      86 để chọn tất cả các phần tử, e. g. ,

      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      7

      Các chức năng trên chọn (các) phần tử dựa trên thuộc tính duy nhất

      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      19,
      for ( initialization ; test ; post-processing ) {
         body ;
      }
      33 và tên thẻ. HTML 5 định nghĩa thêm hai hàm có thể chọn các thành phần dựa trên thuộc tính
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      82 (được CSS sử dụng rộng rãi trong bộ chọn lớp)

      HàmMô tảVí dụ_______36_______90Trả về phần tử đầu tiên với thuộc tính
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      82 đã cho.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      8
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      92Trả về một mảng các phần tử với thuộc tính
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      82 đã cho.
      
      
      
      
        
        JavaScript Example: The Date object
        
      
      
      
      8

      Ngoài các chức năng lựa chọn trên, có nhiều chức năng lựa chọn khác có sẵn. Tuy nhiên, tôi thực sự khuyên bạn nên sử dụng các chức năng trên. Tôi liệt kê bên dưới chức năng khác bên dưới cho đầy đủ

      1. 
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        94 trả về một mảng gồm tất cả các phần tử
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        95, giống như
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        96
      2. 
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        97. trả về một mảng các phần tử
      3. 
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        98 và
        
        
        
        
          
          JavaScript Example: Events onload, onmouseover and onmouseout
          
        
        
          

        "Hello" alert Box appears after the page is loaded.

        Point your mouse pointer here!!!

        99. trả về tất cả các siêu liên kết
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        6và các phần tử neo
        
        
        
        
          
          JavaScript Example: User-defined function and onclick Event Handler
          
        
        
          

        Example on event and user-defined function

        6. [Xác nhận. ]

      Thao tác nội dung của phần tử thông qua thuộc tính
      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      42

      Bạn có thể truy cập và sửa đổi nội dung của một phần tử thông qua thuộc tính "_______59_______42", chứa tất cả các văn bản (bao gồm các thẻ lồng nhau) trong phần tử này

      Ví dụ,

      "______59_______42" là cách thuận tiện nhất để truy cập và thao tác nội dung của một phần tử. Tuy nhiên, nó không phải là một tiêu chuẩn W3C, nhưng nó được hầu hết các trình duyệt hỗ trợ

      Cây DOM và nút

      W3C khuyên bạn nên truy cập và thao tác các phần tử HTML thông qua cây DOM và các nút. Tuy nhiên, nó thực sự là quá mức cần thiết để viết các JavaScript đơn giản. Tôi trình bày chúng ở đây một lần nữa cho đầy đủ

      Khi một trình duyệt tải một trang HTML, nó sẽ xây dựng mô hình DOM một trang web theo cấu trúc giống như cây phân cấp bao gồm các nút, giống như cấu trúc HTML của nó

      Một ví dụ về tài liệu HTML với cây DOM tương ứng được đưa ra sau. Hãy lưu ý rằng nội dung văn bản của một phần tử được lưu trữ trong một nút Văn bản riêng biệt

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      0
      Javascript kiểm tra xem Internalhtml có chứa chuỗi không

      Tải trang web lên Firefox và sử dụng firebug để kiểm tra cây DOM.

      Cây DOM bao gồm các loại nút sau

      1. nút tài liệu. nút gốc đại diện cho toàn bộ tài liệu HTML
      2. nút phần tử. đại diện cho một phần tử HTML (hoặc thẻ). Một nút phần tử có thể có các nút con, có thể là nút phần tử hoặc nút văn bản. Nút phần tử cũng có thể có các thuộc tính
      3. nút văn bản. chứa nội dung văn bản của một phần tử
      4. Khác. chẳng hạn như nhận xét, thuộc tính

      Một nút DOM có các thuộc tính này

      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        005. chứa tên của nút, chỉ đọc.
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        005 cho nút Phần tử là tên thẻ;
      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        011. chứa giá trị của nút.
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        011 cho nút Văn bản là văn bản được chứa;
      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        014. một số nguyên cho biết loại nút, e. g. , Phần tử (1), Thuộc tính (2), Văn bản (3), Nhận xét (8), Tài liệu (9)
      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        015. tham chiếu đến nút cha. Chỉ có một nút cha trong cấu trúc cây
      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        016. mảng (hoặc danh sách nút) của các nút con
      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        017,
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        018. tham chiếu đến nút con đầu tiên và cuối cùng
      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        019,
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        020. tham chiếu đến anh chị em trước và sau trong cùng cấp độ

      Lưu ý sự khác biệt giữa thuật ngữ số ít và số nhiều. Ví dụ:

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      015 đề cập đến nút cha (mỗi nút ngoại trừ nút gốc có một và chỉ một nút cha),
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      016 chứa một mảng gồm tất cả các nút con

      Nút gốc của cây DOM được gọi là

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      6. Nút gốc
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      6 chỉ có một nút con, được gọi là
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      025, đại diện cho thẻ và nó đóng vai trò là nút cha cho hai nút con đại diện và thẻ, từ đó sẽ có các nút con khác. Bạn cũng có thể sử dụng thuộc tính đặc biệt có tên là
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      026 để truy cập trực tiếp vào thẻ

      Ví dụ: bạn có thể sử dụng thuộc tính nút sau để truy cập nút Văn bản

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      027 trong ví dụ trên

      Thí dụ

      JavaScript sau liệt kê tất cả các nút trong phần, theo cách tìm kiếm chuyên sâu, thông qua hàm đệ quy

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      1
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      2

      Việc truy cập phần tử HTML thông qua giao diện Node có thể không quá hữu ích cũng như không thực tế đối với các ứng dụng JavaScript, vì bạn cần biết cấu trúc tô pô thực tế của cây DOM. Hơn nữa, một số trình duyệt (e. g. , firefox) có thể tạo thêm các nút Văn bản để chứa khoảng trắng giữa các thẻ

      nút văn bản

      DOM mô hình hóa các văn bản được bao quanh bởi các thẻ HTML dưới dạng một nút văn bản riêng biệt. Nó không thể có nút con. Để truy xuất nội dung văn bản, bạn có thể thuộc tính

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      011. Ví dụ,

      thuộc tính thuộc tính

      Để truy cập thuộc tính của nút Phần tử có tên là

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      029, bạn có thể sử dụng

      • thuộc tính
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        030, trong đó
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        031 là tên của thuộc tính, hoặc
      • phương pháp
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        032 và
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        033

      Ví dụ,

      Thuộc tính
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      034 (đối với CSS)

      Phần tử có một thuộc tính được gọi là kiểu, mô hình kiểu CSS với các thuộc tính CSS chẳng hạn như

      alert('hello, world')
      document.write('hello world, again')
      console.log('hello world, again and again')
      07 và
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      036. Ví dụ,

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      3

      Thao túng các nút

      Một đối tượng Node có các chức năng này

      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        037. trả về
        My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        26 nếu nút này có ít nhất một nút con
      • Thao tác nút con
        • My First JavaScript says
          

          Hello world, again!

          This document was last modified on mm/dd/yyyy hh:mm:ss.

          039. chèn một nút trước một nút con hiện có
        • My First JavaScript says
          

          Hello world, again!

          This document was last modified on mm/dd/yyyy hh:mm:ss.

          040. thay thế một nút con hiện có
        • My First JavaScript says
          

          Hello world, again!

          This document was last modified on mm/dd/yyyy hh:mm:ss.

          041. xóa nút con đã chỉ định
        • My First JavaScript says
          

          Hello world, again!

          This document was last modified on mm/dd/yyyy hh:mm:ss.

          042. nối thêm nút đã cho làm nút con cuối cùng
      • My First JavaScript says
        

        Hello world, again!

        This document was last modified on mm/dd/yyyy hh:mm:ss.

        043______46_______044
      Tạo một phần tử mới (_______46_______045) và Nút văn bản (
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      046), Nối một nút (
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      047)

      Để tạo nút văn bản mới, bạn có thể sử dụng

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      048 để tạo một nút văn bản độc lập, tiếp theo là
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      049
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      050 để nối nút văn bản vào một phần tử

      Tương tự, bạn có thể sử dụng

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      051 để tạo một phần tử độc lập, theo sau là một
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      052 để nối phần tử đã tạo vào một phần tử hiện có

      Ví dụ, chúng ta sẽ tạo một nút văn bản mới, như một nút con của một nút mới.

      yếu tố. Sau đó chúng ta sẽ nối cái mới

      phần tử là phần tử con cuối cùng của

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      4
      Chèn một nút mới (
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      053)
      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      4
      Thay thế một nút (
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      054)

      Thay đổi dòng cuối cùng thành

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      055

      Xóa một nút (
      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      056)

      Bạn có thể xóa nút con khỏi nút cha thông qua

      My First JavaScript says
      

      Hello world, again!

      This document was last modified on mm/dd/yyyy hh:mm:ss.

      057

      Ví dụ: hãy loại bỏ cuối cùng

      từ

      if ( condition-1 ) {
         block-1 ;
      } else if ( condition-2 ) {
         block-2 ;
      } else if ( condition-3 ) {
         block-3 ;
      ......
      ......
      } else {
         block-n ;
      }
      6

      Đối tượng
      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      6

      Đối tượng

      
      
      
      
        
        JavaScript Example: Events onload, onmouseover and onmouseout
        
      
      
        

      "Hello" alert Box appears after the page is loaded.

      Point your mouse pointer here!!!

      6 là nút gốc của cây DOM. Nó có thể được sử dụng để truy cập tất cả các phần tử trong trang HTML

      Làm cách nào để kiểm tra xem phần tử có chứa chuỗi trong JavaScript không?

      Phương thức include() trả về giá trị true nếu một chuỗi chứa một chuỗi đã chỉ định . Nếu không nó trả về false. Phương thức bao gồm () phân biệt chữ hoa chữ thường.

      Làm cách nào để kiểm tra xem div có chứa văn bản không?

      phương thức include() có thể được sử dụng để kiểm tra xem phần tử DIV (hoặc bất kỳ phần tử nào) có chứa một văn bản hoặc từ cụ thể trong đó không.

      Làm cách nào để kiểm tra văn bản JavaScript?

      Bạn có thể kiểm tra xem chuỗi JavaScript có chứa ký tự hoặc cụm từ hay không bằng cách sử dụng phương thức bao gồm(), indexOf() hoặc biểu thức chính quy. .
      bao gồm()
      Chỉ số()
      Biểu thức chính quy (regex)

      Làm cách nào để lấy giá trị của InternalHTML trong JavaScript?

      Cách thức hoạt động. .
      First, get the
        element with the id menu using the getElementById() method..
      Second, create a new
    4. element and add it to the
        element using the createElement() and appendChild() methods..
    5. Third, get the HTML of the
        element using the innerHTML property of the
          element..