迅雷今天前端笔试编程-bind

。。不知道哪里出问题了通过率0
下面我们需要构造一个新的特殊的bind方法,覆盖 Function.prototype.bind 方法,要求如下:

  • 新的bind方法返回的函数拷贝需要支持再次改变this的指向
  • 新的bind方法需要像ES5标准的bind方法一样处理预设参数和函数拷贝的动态参数

答题方法

  1. 选择 JavaScript(Node 0.12.12),其他语言通过不得分
    2.复制起手代码
    var repl = require('repl')

Function.prototype.bind = function () {
// TODO
}

repl.start({
useGlobal: true,
prompt: ''
})
3.编写代码并保存
输入描述:
给定的函数被覆盖为bind后的函数拷贝并执行
输出描述:
函数拷贝的执行结果
示例1
输入

function foo() {
return this.bar
}

foo = foo.bind({ bar: 'bar1' })
foo = foo.bind({ bar: 'bar2' })

foo()
输出

'bar2'
示例2
输入

function foo(a, b, c) {
return a + " " + b + " " + c + " " + this.bar
}

foo = foo.bind({
bar: "bar6"
}, 3, 4)

foo(5)
输出

'3 4 5 bar6'
、、、、、、、、
我的代码

Function.prototype.bind = function () {
// 这里写代码
    var _this = arguments[0];


    var data =Array.prototype.slice.apply(arguments,[1]);
    var self = this;
    return function(){

       return self.apply(_this,data.concat(Array.prototype.slice.apply(arguments,[0])));
    }
}

求告诉

#迅雷##前端工程师#
全部评论
我的过了,只是把第一次this状态保存,方法比较取巧 https://www.nowcoder.com/discuss/44541?type=0&order=0&pos=26&page=0
点赞 回复
分享
发布于 2017-09-19 20:36
你这个不能链式调用bind吧
点赞 回复
分享
发布于 2017-09-19 20:38
联易融
校招火热招聘中
官网直投

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务