Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript
JavaScript
  1. แสดงรายการอาเรย์ทีละรายการแบบย้อนกลับเมื่อคลิกปุ่มใน JavaScript

    ใช้ตัวนับเริ่มจาก Index สุดท้าย ลดลงทีละ 1 ทุกครั้งที่คลิก กลับไปสุดท้ายเมื่อหมด ตัวอย่างโค้ด <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8> <meta name=viewport content=width=device-width, initial-scale=1.0> <title>Document</title> &

  2. สร้าง Associative Array ใน JavaScript

    Associative Array ใน JS คือ Object ที่ใช้ Key แทน Index ใช้ Array of Objects ตัวอย่างโค้ด var customerDetails = [ { customerId: customer-1, customerName: David, customerCountryName: US }, { customerId: customer-2, customerName: Bob, customerCountryName: UK }, { customerId: customer-3, c

  3. สร้าง Associative Array ด้วย push() ใน JavaScript

    ใช้ forEach() วนอาเรย์ สร้าง Object ที่ Key คือ studentId Value เป็นอาเรย์ชื่อ ตัวอย่างโค้ด var studentDetails = [ { studentId: 1, studentName: John }, { studentId: 1, studentName: David }, { studentId: 2, studentName: Bob }, { studentId: 2, studentName: Carol } ]; studentObject =

  4. HTML Form Action และ OnSubmit Validation ด้วย JavaScript

    ใช้ onsubmit=return validateFunction() ป้องกัน Submit ถ้า Validation ไม่ผ่าน ตัวอย่างโค้ด <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8> <meta name=viewport content=width=device-width, initial-scale=1.0> <title>Document</title> </

  5. หา Key Name เมื่อ Value ว่างใน Object ด้วย JavaScript

    ใช้ Object.keys() และ find() หา Key ที่ Value เป็น หรือ null ตัวอย่างโค้ด (details[key] === || details[key] === null)); console.log(The key is= + result); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo118.js) ผลลัพธ์ The key is=lastName

  6. ใช้ map() เข้าถึง Nested Object ใน JavaScript

    ใช้ map() วนอาเรย์ ตรวจสอบ typeof nestedObject !== undefined ก่อนเข้าถึง Property ซ้อน ตัวอย่างโค้ด { if (typeof nestedObject.subjectDetails != undefined) { console.log(The subject Name= + nestedObject.subjectDetails.subjectName); } }); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เ

  7. Submit Textbox เมื่อกดปุ่ม Enter ใน JavaScript

    ใช้ Event keydown ตรวจสอบ keyCode === 13 (Enter) เรียกฟังก์ชัน Submit ตัวอย่างโค้ด <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8> <meta name=viewport content=width=device-width, initial-scale=1.0> <title>Document</title> </head>

  8. ความแตกต่างระหว่าง Hibernate และ EclipseLink

    ทั้งสองเป็น ORM (Object-Relational Mapping) Implement ของ JPA (Java Persistence API) ภาพรวม Hibernate: Implementation ยอดนิยมของ JPA พัฒนาโดย Red Hat มีฟีเจอร์เพิ่มเติมเกิน JPA Spec EclipseLink: Open Source JPA Implementation โดย Eclipse Foundation เป็นโปรเจกต์แรกของ EE4J มี 2 รูปแบบ: JAR

  9. สร้าง Custom Find Function คล้าย find() ใน JavaScript

    เขียนฟังก์ชันเองวนลูปหา Object ที่ Property ตรงกับค่าที่ต้องการ ตัวอย่างโค้ด const studentDetails = [ { studentId: 101, studentName: John }, { studentId: 102, studentName: David }, { studentId: 103, studentName: Carol } ]; function findByName(name) { var flag = false; for (var

  10. ใส่ตัวแปรใน Regular Expression Match ด้วย JavaScript

    ใช้ new RegExp(variable, flags) สร้าง Regex จากตัวแปร แล้วใช้ match() ตัวอย่างโค้ด var sentence = My Name is John; console.log(The actual value=); console.log(sentence); var matchWord = John; console.log(The matching value=); console.log(matchWord); var matchRegularExpression = new RegExp(matchWo

  11. ตรวจสอบว่าสตริงมีช่องว่างหรือไม่ใน JavaScript

    ใช้ indexOf( )= 0 แปลว่ามีช่องว่าง ตัวอย่างโค้ด = 0; } var whiteSpace = stringHasTheWhiteSpaceOrNot(MyNameis John); if (whiteSpace == true) { console.log(The string has whitespace); } else { console.log(The string does not have whitespace); } วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช

  12. นับจำนวน Element ในอาเรย์ด้วย Recursive Function ใน JavaScript

    ฟังก์ชันเรียกตัวเอง (Recursive) กรณีฐาน: อาเรย์ว่าง คืน 0 มิฉะนั้น 1 + เรียกฟังก์ชันต่อกับ slice(1) ตัวอย่างโค้ด function countNumberOfElementsUsingRecursive(listOfMarks) { if (listOfMarks.length == 0) { return 0; } return 1 + countNumberOfElementsUsingRecursive(listOfMarks.slic

  13. แสดงสถานะเปิด/ปิดสำนักงานจากเวลาปัจจุบันด้วย Ternary Operator

    ใช้ new Date().getHours() ดึงชั่วโมงปัจจุบัน แล้ว Ternary Operator ตัดสินใจ ตัวอย่างโค้ด = 10 && gettingHours < 18) ? Open : Closed; document.querySelector(.closeOrOpened).innerHTML = actualHours; </script> </body> </html> วิธีรันโปรแกรม บันทึกไฟล์ชื่อ anyName.ht

  14. สร้างอาเรย์ใหม่จากอาเรย์เก่าโดยไม่กระทบต้นฉบับใน JavaScript

    ใช้ slice() หรือ JSON.parse(JSON.stringify()) สำเนาอาเรย์ Deep Copy ตัวอย่างโค้ด function createNewArray(listOfNames) { return JSON.parse(JSON.stringify(listOfNames)); } var listOfNames = [John, Mike, Sam, Carol]; var namesArray = listOfNames.slice(); // Shallow Copy พอสำหรับ Primitive consol

  15. จัดรูปแบบข้อความเป็นตารางด้วย New Line ใน JavaScript

    ใช้ map() สร้างแถว แล้ว join(\n) รวมด้วย New Line ตัวอย่างโค้ด `|${obj.join(|)}|`).join(\n); studentFormat = studentFormat + separate; console.log(studentFormat); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo112.js) ผลลัพธ์ ||Id||Name||subjName|| |101|John|JavaScript| |102|Bob|MySQL

  16. สร้าง JavaScript Object จาก Single Array กำหนด Key-Value เอง

    ใช้ Object.entries() และ map() แปลง Object เป็นอาเรย์ของ Object ที่มี Key-Value ชัดเจน ตัวอย่างโค้ด ({ studentId, studentName })); console.log(studentDetails); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo113.js) ผลลัพธ์ [ { studentId: 101, studentName: John }, { student

Total 5941 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:106/298  20-คอมพิวเตอร์/Page Goto:1 100 101 102 103 104 105 106 107 108 109 110 111 112