Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端日常开发总结 #19

Open
cookiepool opened this issue Nov 4, 2019 · 0 comments
Open

前端日常开发总结 #19

cookiepool opened this issue Nov 4, 2019 · 0 comments

Comments

@cookiepool
Copy link
Owner

cookiepool commented Nov 4, 2019

1、如何判断一个对象为空

1.1 使用JSON.stringify()

把JS对象转换为JSON字符串再比较

    let a = {name: 'lee'};
    if(JSON.stringify(a) === '{}'){
        console.log('这是空对象');
    }else {
        console.log('这不是是空对象');
    }

1.2 使用for in

利用for in的特性,如果不是空对象,那一定会进循环部分里面

    let a = {};
    let isEmptyObj = true;
    for(let key in a) {
        isEmptyObj = false;
    }
    if(isEmptyObj === true) {
        console.log('这是空对象');
    }else {
        console.log('这不是空对象');
    }

1.3 使用ES6 Object.keys

Object.keys() 方法会返回一个由给定对象的自身可枚举属性组成的数组。 如果我们的对象为空,他会返回一个空数组

    let a = {name: 'lee'};
    if(Object.keys(a).length === 0) {
        console.log('这是一个空对象');
    }else {
        console.log('这不是一个空对象');
    }

2、如何将类数组对象转换为数组

我以arguments对象为例

2.1、使用slice与apply

Array.prototype.slice.apply(arguments);

2.2、使用Array.from()

Array.from(arguments);

2.3、使用展开运算符 ...

let newArr = [...arguments];
@cookiepool cookiepool changed the title 测试 前端日常开发总结 Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant