データ型の検証

typeof

typeofを使用してデータ型を決定すると、結果は次のようになります。

console.log(
    typeof 123, //"number"
    typeof 'dsfsf', //"string"
    typeof false, //"boolean"
    typeof [1,2,3], //"object"
    typeof {
    
    a:1,b:2,c:3}, //"object"
    typeof function(){
    
    console.log('aaa');}, //"function"
    typeof undefined, //"undefined"
    typeof null, //"object"
    typeof new Date(), //"object"
    typeof /^[a-zA-Z]{
    
    5,20}$/, //"object"
    typeof new Error() //"object"
);

上記の結果はChromeブラウザで実行した結果です。次のルールを見つけることができます

Array、Object、null、Date、RegExp、Errorはすべてtypeofによってオブジェクトとして判断されるため、これらのタイプを判断する場合は、typeofを使用できません。

Number、String、Boolean、Function、undefined、これらのタイプを判断する場合は、typeofを使用できます。

instanceof

typeofを使用して判断することに加えて、instanceofを使用することもできます。instanceof演算子は、コンストラクターを指定するか、特定のタイプを指定する必要があります。これは、このコンストラクターのプロトタイプが特定のオブジェクトのプロトタイプチェーン上にあるかどうかを判断するために使用されます。

結果は次のとおりです。

console.log(
    123 instanceof Number, //false
    'dsfsf' instanceof String, //false
    false instanceof Boolean, //false
    [1,2,3] instanceof Array, //true
    {
    
    a:1,b:2,c:3} instanceof Object, //true
    function(){
    
    console.log('aaa');} instanceof Function, //true
    undefined instanceof Object, //false
    null instanceof Object, //false
    new Date() instanceof Date, //true
    /^[a-zA-Z]{
    
    5,20}$/ instanceof RegExp, //true
    new Error() instanceof Error //true
)

次のルールがあります。

Number、String、Booleanはそれらのタイプを検出しませんでしたが、次の表記を使用すると検出できます。

var num = new Number(123);
var str = new String('dsfsf');
var boolean = new Boolean(false);

また、nullとundefinedの両方がfalseを返すことに注意してください。これは、それらのタイプがそれ自体であり、Objectが作成したものではないため、falseが返されるためです。

constructor

コンストラクターはプロトタイプオブジェクトのプロパティであり、コンストラクターを指します。インスタンスオブジェクトが属性を検索する順序に従って、インスタンスオブジェクトにインスタンス属性またはメソッドがない場合、それらはプロトタイプチェーンで検索されます。したがって、インスタンスオブジェクトはコンストラクター属性を使用することもできます。

型のインスタンスのコンストラクターを出力すると、次のようになります。

console.log(new Number(123).constructor)
//ƒ Number() { [native code] }

Numberのコンストラクターを指していることがわかります。したがって、num.constructor == Numberを使用して、変数がNumber型であるかどうかを判別できます。

var num  = 123;
var str  = 'abcdef';
var bool = true;
var arr  = [1, 2, 3, 4];
var json = {
    
    name:'wenzi', age:25};
var func = function(){
    
     console.log('this is function'); }
var und  = undefined;
var nul  = null;
var date = new Date();
var reg  = /^[a-zA-Z]{5,20}$/;
var error= new Error();

function Person(){
    
    
  
}
var tom = new Person();

// undefined和null没有constructor属性
console.log(
    tom.constructor==Person,
    num.constructor==Number,
    str.constructor==String,
    bool.constructor==Boolean,
    arr.constructor==Array,
    json.constructor==Object,
    func.constructor==Function,
    date.constructor==Date,
    reg.constructor==RegExp,
    error.constructor==Error
);
//所有结果均为true

undefinedとnullを除いて、他のタイプはコンストラクター属性によって判断できます。

toString()を使用してオブジェクトタイプを検出します

toString()を介して各オブジェクトのタイプを取得できます。Object.prototype.toString()によって各オブジェクトが検出されるためには、Function.prototype.call()またはFunction.prototype.apply()の形式で呼び出され、チェック対象のオブジェクトが最初のパラメーター、thisArgと呼ばれます。

var toString = Object.prototype.toString;

toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({
    
    name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){
    
     console.log('this is function'); }); //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"

Object.prototype.toString.call()を使用して変数の型を判別することが、最も正確なメソッドであることがわかります。

関数をカプセル化して、変数の正確な型を取得します

function gettype(obj) {
    
    
  var type = typeof obj;

  if (type !== 'object') {
    
    
    return type;
  }
  //如果不是object类型的数据,直接用typeof就能判断出来

  //如果是object类型数据,准确判断类型必须使用Object.prototype.toString.call(obj)的方式才能判断
  return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}

このように変数のデータ型を判断すると非常に便利です。

おすすめ

転載: blog.csdn.net/weixin_53687450/article/details/112547872