- isArray
var arr = [1,2,3];
console.log( Array.isArray( arr ) );
javascript
<script>
var arr = [1,2,3];
var str = '你好';
console.log( Array.isArray( arr ) ); // true
console.log( Array.isArray( str ) ); // false
</script>
- instanceof
var arr = [1,2,3];
console.log( arr instanceof Array );
javascript
<script>
var arr = [1,2,3];
var str = '你好';
console.log( arr instanceof Array ); // true
console.log( str instanceof Array ) ); // false
</script>
- prototype
var arr = [1,2,3];
console.log( typeof Object.prototype.toString.call(arr).indexOf(Array) != -1 );
javascript
<script>
var arr = [1,2,3];
var str = '你好';
console.log( Object.prototype.toString.call(arr) ); // 打印出 [object Array]
console.log( typeof Object.prototype.toString.call(arr) ); // 打印出string
console.log( typeof Object.prototype.toString.call(arr).indexOf(Array) ); // 如果不是-1,就证明是数组
console.log( typeof Object.prototype.toString.call(arr).indexOf(Array) != -1 ); // 打印true或false
</script>
- isPrototypeOf
var arr = [1,2,3];
console.log( Array.prototype.isPrototypeOf(arr) );
javascript
<script>
var arr = [1,2,3];
var str = '你好';
console.log( Array.prototype.isPrototypeOf(arr) );
</script>
- constructor
var arr = [1,2,3];
console.log( arr.constructor.toString().indexOf('Array') > -1 );
javascript
<script>
var arr = [1,2,3];
console.log( arr.constructor.toString().indexOf('Array') > -1 );
</script>