【JavaScript】之【Object】

时间:2025-03-12 15:04:25

见代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Object</title> </head>
<body>
<script type="text/javascript">
// let obj = {};
// let arr = [];
var obj = {};
var arr = []; console.log(typeof obj);//object
console.log(typeof arr);//object
console.log(typeof null);//object
console.log(typeof '');//string
console.log(typeof undefined);//undefined console.log('*******************************************'); console.log(typeof obj === 'object');//true
console.log(typeof arr === 'object');//true
console.log(typeof null === 'object');//true
console.log(typeof '' === 'object');//false
console.log(typeof undefined === 'object');//false console.log('*******************************************'); console.log("typeof obj === 'object':" + typeof obj === 'object');//false
console.log("typeof arr === 'object':" + typeof arr === 'object');//false
console.log("typeof null === 'object':" + typeof null === 'object');//false
console.log("typeof '' === 'object':" + typeof '' === 'object');//false
console.log("typeof undefined === 'object':" + typeof undefined === 'object');//false console.log('------------------优先级搞的鬼'); console.log("typeof obj === 'object':" + (typeof obj === 'object'));//typeof obj === 'object':true
console.log("typeof arr === 'object':" + (typeof arr === 'object'));//typeof arr === 'object':true
console.log("typeof null === 'object':" + (typeof null === 'object'));//typeof null === 'object':true
console.log("typeof '' === 'object':" + (typeof '' === 'object'));//typeof '' === 'object':false
console.log("typeof undefined === 'object':" + (typeof undefined === 'object'));//typeof undefined === 'object':false console.log('------------------优先级搞的鬼');
console.log(("typeof obj === 'object':" + typeof obj) === 'object');//false
console.log(("typeof arr === 'object':" + typeof arr) === 'object');//false
console.log(("typeof null === 'object':" + typeof null) === 'object');//false
console.log(("typeof '' === 'object':" + typeof '') === 'object');//false
console.log(("typeof undefined === 'object':" + typeof undefined) === 'object');//false
//"typeof obj === 'object':object“ === 'object' --------false console.log('*******************************************'); console.log("Object.prototype.toString.call(obj) === '[object Object]':" + (Object.prototype.toString.call(obj)));//Object.prototype.toString.call(obj) === '[object Object]':[object Object]
console.log("Object.prototype.toString.call(arr) === '[object Object]':" + (Object.prototype.toString.call(arr)));//Object.prototype.toString.call(arr) === '[object Object]':[object Array]
console.log("Object.prototype.toString.call(null) === '[object Object]':" + (Object.prototype.toString.call(null)));//Object.prototype.toString.call(null) === '[object Object]':[object Null]
console.log("Object.prototype.toString.call('') === '[object Object]':" + (Object.prototype.toString.call('')));//Object.prototype.toString.call('') === '[object Object]':[object String]
console.log("Object.prototype.toString.call(undefined) === '[object Object]':" + (Object.prototype.toString.call(undefined)));// Object.prototype.toString.call(undefined) === '[object Object]':[object Undefined] console.log('*******************************************');
//严谨的判断Object方式:
console.log("Object.prototype.toString.call(obj) === '[object Object]':" + (Object.prototype.toString.call(obj) === "[object Object]"));//Object.prototype.toString.call(obj) === '[object Object]':true
console.log("Object.prototype.toString.call(arr) === '[object Object]':" + (Object.prototype.toString.call(arr) === "[object Object]"));//Object.prototype.toString.call(arr) === '[object Object]':false
console.log("Object.prototype.toString.call(null) === '[object Object]':" + (Object.prototype.toString.call(null) === "[object Object]"));// Object.prototype.toString.call(null) === '[object Object]':false
console.log("Object.prototype.toString.call('') === '[object Object]':" + (Object.prototype.toString.call('') === "[object Object]"));// Object.prototype.toString.call('') === '[object Object]':false
console.log("Object.prototype.toString.call(undefined) === '[object Object]':" + (Object.prototype.toString.call(undefined) === "[object Object]"));//Object.prototype.toString.call(undefined) === '[object Object]':false </script>
</body>
</html>