JS กวดวิชา

เจเอสโฮม JS Introduction JS Where To JS เอาท์พุต งบ JS JS ไวยากรณ์ JS ความคิดเห็น ตัวแปร JS เจเอส เลท JS Const JS Operators JS เลขคณิต JS Assignment ประเภทข้อมูล JS ฟังก์ชัน JS วัตถุ JS JS Events JS Strings JS String Methods JS String Search เทมเพลตสตริง JS JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates รูปแบบวันที่ JS JS วันที่รับเมธอด JS Date Set Methods JS Math JS สุ่ม JS Booleans JS เปรียบเทียบ JS Conditions JS Switch JS ลูปสำหรับ JS Loop สำหรับ In JS วนสำหรับ Of JS วนรอบในขณะที่ JS Break JS Iterables JS ชุด JS Maps JS Typeof JS ประเภทการแปลง JS Bitwise JS RegExp JS Errors JS ขอบเขต JS Hoisting JS โหมดเข้มงวด JS คำสำคัญนี้ JS Arrow Function JS Classes JS JSON JS Debugging คู่มือสไตล์ JS JS แนวทางปฏิบัติที่ดีที่สุด JS ความผิดพลาด JS Performance JS สงวนคำ

เวอร์ชัน JS

เวอร์ชัน JS JS 2009 (ES5) เจเอส 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / ขอบ JS History

วัตถุ JS

คำจำกัดความของวัตถุ คุณสมบัติของวัตถุ วิธีการวัตถุ การแสดงวัตถุ อุปกรณ์เสริมวัตถุ ตัวสร้างวัตถุ ต้นแบบวัตถุ วัตถุ Iterables ชุดวัตถุ แผนที่วัตถุ การอ้างอิงวัตถุ

ฟังก์ชัน JS

คำจำกัดความของฟังก์ชัน พารามิเตอร์ฟังก์ชัน เรียกใช้ฟังก์ชัน เรียกฟังก์ชัน ฟังก์ชัน สมัคร การปิดฟังก์ชัน

JS Classes

แนะนำคลาส การสืบทอดคลาส คลาสคงที่

JS Async

JS Callbacks JS แบบอะซิงโครนัส JS Promises JS Async/รอ

JS HTML DOM

บทนำ DOM วิธี DOM เอกสาร DOM องค์ประกอบ DOM DOM HTML แบบฟอร์ม DOM DOM CSS ภาพเคลื่อนไหว DOM กิจกรรม DOM DOM Event Listener การนำทาง DOM โหนด DOM DOM Collections รายการโหนด DOM

JS เบราว์เซอร์ BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

แนะนำ Web API เว็บฟอร์ม API API ประวัติเว็บ API ที่เก็บข้อมูลเว็บ Web Worker API API ดึงเว็บ เว็บ Geolocation API

JS AJAX

บทนำ AJAX AJAX XMLHttp คำขอ AJAX การตอบสนอง AJAX ไฟล์ AJAX XML AJAX PHP AJAX ASP ฐานข้อมูล AJAX แอปพลิเคชัน AJAX ตัวอย่าง AJAX

JS JSON

บทนำ JSON ไวยากรณ์ JSON JSON กับ XML ประเภทข้อมูล JSON JSON Parse JSON สตริงริฟาย วัตถุ JSON อาร์เรย์ JSON เซิร์ฟเวอร์ JSON JSON PHP JSON HTML JSON JSONP

JS กับ jQuery

ตัวเลือก jQuery jQuery HTML jQuery CSS jQuery DOM

JS กราฟิก

JS กราฟิก JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js

ตัวอย่าง JS

ตัวอย่าง JS JS HTML DOM JS HTML อินพุต JS HTML Objects JS HTML Events JS Browser JS Editor JS แบบฝึกหัด JS Quiz ใบรับรอง JS

JS References

วัตถุ JavaScript วัตถุ HTML DOM


ประเภทจาวาสคริปต์ของ


ใน JavaScript มีประเภทข้อมูลที่แตกต่างกัน 5 ประเภทที่สามารถมีค่าได้:

  • string
  • number
  • boolean
  • object
  • function

วัตถุมี 6 ประเภท:

  • Object
  • Date
  • Array
  • String
  • Number
  • Boolean

และ 2 ประเภทข้อมูลที่ไม่สามารถมีค่าได้:

  • null
  • undefined

ประเภทของโอเปอเรเตอร์

คุณสามารถใช้ตัวtypeofดำเนินการเพื่อค้นหาชนิดข้อมูลของตัวแปร JavaScript

ตัวอย่าง

typeof "John"                 // Returns "string"
typeof 3.14                   // Returns "number"
typeof NaN                    // Returns "number"
typeof false                  // Returns "boolean"
typeof [1,2,3,4]              // Returns "object"
typeof {name:'John', age:34}  // Returns "object"
typeof new Date()             // Returns "object"
typeof function () {}         // Returns "function"
typeof myCar                  // Returns "undefined" *
typeof null                   // Returns "object"

โปรดสังเกต:

  • ชนิดข้อมูลของ NaN คือ number
  • ชนิดข้อมูลของอาร์เรย์คือ object
  • ชนิดข้อมูลของวันที่เป็นวัตถุ
  • ชนิดข้อมูลของ null คือ object
  • ชนิดข้อมูลของตัวแปรที่ไม่ได้กำหนดนั้นไม่ได้กำหนดไว้ *
  • ชนิดข้อมูลของตัวแปรที่ยังไม่ได้กำหนดค่านั้นไม่ได้กำหนดไว้ เช่นกัน *

คุณไม่สามารถใช้typeofเพื่อกำหนดว่าวัตถุ JavaScript เป็นอาร์เรย์ (หรือวันที่)



ข้อมูลดั้งเดิม

ค่าข้อมูลดั้งเดิมเป็นค่าข้อมูลอย่างง่ายเพียงค่าเดียวที่ไม่มีคุณสมบัติและวิธีการเพิ่มเติม

โอtypeofเปอเรเตอร์สามารถส่งคืนหนึ่งในประเภทดั้งเดิมเหล่านี้:

  • string
  • number
  • boolean
  • undefined

ตัวอย่าง

typeof "John"              // Returns "string"
typeof 3.14                // Returns "number"
typeof true                // Returns "boolean"
typeof false               // Returns "boolean"
typeof x                   // Returns "undefined" (if x has no value)

ข้อมูลที่ซับซ้อน

ตัวtypeofดำเนินการสามารถส่งคืนหนึ่งในสองประเภทที่ซับซ้อน:

  • function
  • object

ตัวtypeofดำเนินการส่งคืน "วัตถุ" สำหรับอ็อบเจ็กต์ อาร์เรย์ และค่า null

ตัวtypeofดำเนินการไม่ส่งคืน "วัตถุ" สำหรับฟังก์ชัน

ตัวอย่าง

typeof {name:'John', age:34} // Returns "object"
typeof [1,2,3,4]             // Returns "object" (not "array", see note below)
typeof null                  // Returns "object"
typeof function myFunc(){}   // Returns "function"

โอtypeofเปอเรเตอร์ส่งคืน " object" สำหรับอาร์เรย์ เนื่องจากในอาร์เรย์ JavaScript เป็นอ็อบเจ็กต์


ประเภทข้อมูลของ typeof

ตัวtypeofดำเนินการไม่ใช่ตัวแปร เป็นผู้ดำเนินการ ตัวดำเนินการ ( + - * / ) ไม่มีประเภทข้อมูลใดๆ

แต่ตัวtypeofดำเนินการจะส่งกลับสตริง เสมอ (ซึ่งมีประเภทของตัวถูกดำเนินการ)


คอนสตรัคเตอร์ พร็อพเพอร์ตี้

คุณสมบัติconstructorส่งคืนฟังก์ชันตัวสร้างสำหรับตัวแปร JavaScript ทั้งหมด

ตัวอย่าง

"John".constructor                // Returns function String()  {[native code]}
(3.14).constructor                // Returns function Number()  {[native code]}
false.constructor                 // Returns function Boolean() {[native code]}
[1,2,3,4].constructor             // Returns function Array()   {[native code]}
{name:'John',age:34}.constructor  // Returns function Object()  {[native code]}
new Date().constructor            // Returns function Date()    {[native code]}
function () {}.constructor        // Returns function Function(){[native code]}

คุณสามารถตรวจสอบคุณสมบัติ Constructor เพื่อดูว่าวัตถุนั้นเป็นArray (มีคำว่า "Array" หรือไม่):

ตัวอย่าง

function isArray(myArray) {
  return myArray.constructor.toString().indexOf("Array") > -1;
}

หรือง่ายกว่านั้น คุณสามารถตรวจสอบได้ว่าวัตถุนั้นเป็นฟังก์ชัน Array หรือไม่ :

ตัวอย่าง

function isArray(myArray) {
  return myArray.constructor === Array;
}

คุณสามารถตรวจสอบคุณสมบัติ Constructor เพื่อดูว่าวัตถุนั้นเป็น a Date(มีคำว่า "Date" หรือไม่):

ตัวอย่าง

function isDate(myDate) {
  return myDate.constructor.toString().indexOf("Date") > -1;
}

หรือง่ายกว่านั้น คุณสามารถตรวจสอบได้ว่าวัตถุนั้นเป็นฟังก์ชัน Date หรือไม่ :

ตัวอย่าง

function isDate(myDate) {
  return myDate.constructor === Date;
}


ไม่ได้กำหนด

undefinedใน JavaScript ตัวแปรที่ไม่มีค่าจะ มีค่า ประเภทยังเป็นundefined.

ตัวอย่าง

let car;    // Value is undefined, type is undefined

ตัวแปรใดๆ สามารถล้างข้อมูลได้โดยการตั้งค่าundefinedเป็น ประเภทก็จะเป็นundefined.

ตัวอย่าง

car = undefined;    // Value is undefined, type is undefined

ค่าว่าง

ค่าว่างไม่มีส่วนเกี่ยวข้องกับundefined.

สตริงว่างมีทั้งค่าทางกฎหมายและประเภท

ตัวอย่าง

let car = "";    // The value is "", the typeof is "string"

โมฆะ

ใน JavaScript nullคือ "ไม่มีอะไร" มันควรจะเป็นสิ่งที่ไม่มีอยู่จริง

ขออภัย ใน JavaScript ชนิดข้อมูลของnullเป็นวัตถุ

คุณสามารถพิจารณาว่าเป็นข้อบกพร่องใน JavaScript ที่typeof nullเป็นวัตถุ มันควรจะnullเป็น

คุณสามารถล้างวัตถุโดยตั้งค่าเป็นnull:

ตัวอย่าง

let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;    // Now value is null, but type is still an object

คุณยังสามารถล้างวัตถุโดยตั้งค่าเป็นundefined:

ตัวอย่าง

let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined;   // Now both value and type is undefined

ความแตกต่างระหว่างไม่ได้กำหนดและ Null

undefinedและnullมีมูลค่าเท่ากันแต่ต่างกันในประเภท:

typeof undefined           // undefined
typeof null                // object

null === undefined         // false
null == undefined          // true