JavaScript isArray

(If you make a mistake, you can stand up immediately; if you lose your trust, you may never be able to recover. - (US) Franklin)

insert image description here

foreword

Before es5, the traditional method of judging arrays was mostly .length or Object.prototype.toString.call().
When using .length to judge, we also need to judge the attribute itself first, so as to avoid undefind errors.
Using Object.prototype.toString.call() can directly judge the array, but the writing method is more complicated.

Therefore, the Array.isArray method was introduced in the es5 specification to quickly judge the array.
Array.isArray
The following is a source code example

Array.isArray = function(arg) {
    
    
  return Object.prototype.toString.call(arg) === '[object Array]';
};

It can be seen that Object.prototype.toString.call() is directly used internally. Simpler than native writing.

おすすめ

転載: blog.csdn.net/qq_42427109/article/details/130500936