ประเภทจาวาสคริปต์ของ
ใน 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