So zeigen Sie Datentypen in JavaScript an

JS-Datentypen

Grundlegende Datentypen: String, Number, Null, Undefiniert, [Boolean]
Referenzdatentypen: Object, Array, Function

So zeigen Sie Datentypen in JavaScript an

  • Wenn Sie typeof zum Anzeigen von Datentypen verwenden, sind Null und Arrays Objekttypen.
  • Die Methode Object.prototype.toString.call() ist die detaillierteste.
  • Die Methode von „constructor.name“ kann null und undefiniert nicht prüfen.

1. Variablentyp

console.log(typeof 1);//number
console.log(typeof "HELLO WORLD");//string
console.log(typeof null);//object
console.log(typeof undefined);//undefined
console.log(typeof true);//Boolean
console.log(typeof false);//Boolean
console.log(typeof {
    
    name:"xiaoming"});//object
console.log(typeof function(){
    
    });//function

Zwei, Object.prototype.toString.call (Variable)

console.log(Object.prototype.toString.call(1));//[object Number]
console.log(Object.prototype.toString.call("HELLO WORLD"));//[object String]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(false));//[object Boolean]
console.log(Object.prototype.toString.call({
    
    name:"xiaoming"}));[object Object]
console.log(Object.prototype.toString.call(function(){
    
    }));//[object Function]
console.log(Object.prototype.toString.call([1,2,3,"a"]));//[object Array]

Drei, variabler Konstruktor

let a=1;
let b="HELLO WORLD";
let c=null;
let d=undefined;
let e=true;
let f=false;
let g={
    
    name:"xiaoming"};
let h=function(){
    
    };
let i=[1,2,3,"a"];
console.log(a.constructor.name);//Number
console.log(b.constructor.name);//String
//console.log(c.constructor.name);//报错:'constructor' of null
//console.log(d.constructor.name);//报错:'constructor' of undefined
console.log(e.constructor.name);//Boolean
console.log(f.constructor.name);//Boolean
console.log(g.constructor.name);//Object
console.log(h.constructor.name);//Function
console.log(i.constructor.name);//Array

Je suppose que tu aimes

Origine blog.csdn.net/qq_40829962/article/details/128238170
conseillé
Classement