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

实现一个函数clone #3

Open
likunNet opened this issue Jun 8, 2017 · 1 comment
Open

实现一个函数clone #3

likunNet opened this issue Jun 8, 2017 · 1 comment

Comments

@likunNet
Copy link
Owner

likunNet commented Jun 8, 2017

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

@koakumaping
Copy link

koakumaping commented Jun 12, 2017

       let newobj
        let str

        if (typeof obj !== 'object') {
            return
        } else if (window.JSON) {
            str = JSON.stringify(obj)
            newobj = JSON.parse(str)
        } else {
            for (const i in obj) {
                if ({}.hasOwnProperty.call(obj, i)) {
                    newobj[i] = typeof obj[i] === 'object' ?
                    this.clone(obj[i]) : obj[i]
                }
            }
        }

        return newobj

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

2 participants