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

深信服:实现一个go()函数(二面) #2

Open
icyfe opened this issue Apr 29, 2019 · 0 comments
Open

深信服:实现一个go()函数(二面) #2

icyfe opened this issue Apr 29, 2019 · 0 comments

Comments

@icyfe
Copy link

icyfe commented Apr 29, 2019

To:
icyfe

面试公司:
深信服

面试环节:
二面

问题:
实现
go('l')//gol
go()('l')//gool
go()()()('l');//返回goool

自己的答案:

实现如下:

 function go(...arg1) {
    let str = 'go'
    var addarg = function (...arg2) {
        if (!arg2[0]) {
            str += 'o' //当没有参数时候就累加默认的参数o 并返回当前函数保持对str的引用产生闭包
            return addarg
        } else {
            return str += arg2[0]//如果是有参数就直接返回最后累加的字符串
        }
    }
    return addarg(...arg1)
}
console.log(go('l'));//gol
console.log(go()()()()('l'));//goooool

解答思路:
说下基本的思路,这题考察闭包,原理上就是当没有传入参数时就默认是传‘o’然后累加起来返回函数本身,当最后有参数时就返回之前累计的参数拼接字符传返回。
推荐阅读相关文章函数科里化

@acodercc acodercc changed the title 深信服:实现一个go()函数 To icyfe:实现一个go()函数(深信服) Apr 29, 2019
@acodercc acodercc changed the title To icyfe:实现一个go()函数(深信服) 深信服:实现一个go()函数(二面) May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants