1、回文(数、数组、字符串)判定
function isPalindrome (param) {
let str = param.toString();
return Array.from(str).reverse().join('') === str;
}
console.log(isPalindrome([1, 2, 3])); //false
console.log(isPalindrome([1, 2, 3, 2, 1])); //true
console.log(isPalindrome('123')); //false
console.log(isPalindrome('12321')); //true
console.log(isPalindrome(12321)); //true
2、数组判定
- ES5定义了Array.isArray:
Array.isArray([1,2,3]) //true
- instanceof
[] instanceof Array //true
- Object.propotype.toString
Object.prototype.toString.call([]) === '[object Array]' //true
- constructor属性
[].constructor === Array //true
3、对象判定
- Object.prototype.toString(最好用)
Object.prototype.toString.call({}) === '[object Object]' //true
- constructor
let obj = {};
obj.constructor === Object //true
4、空对象判定
- JSON.stringify
let obj = {};
JSON.stringify(obj) === '{}' //true
- Object.keys()
let obj = {};
Object.keys(obj).length === 0
- Objcet.getOwnPropertyNames()
let obj = {};
Object.getOwnPropertyNames(obj).length === 0 //true
网友评论