美文网首页
前端常见面试题目(一)

前端常见面试题目(一)

作者: 菜菜的小阿允 | 来源:发表于2020-09-03 16:21 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:前端常见面试题目(一)

          本文链接:https://www.haomeiwen.com/subject/ktzwsktx.html