Неожиданная ошибка аргументов с привязкой, когда я переписываю Function.prototype.bind - PullRequest
0 голосов
/ 02 августа 2020

Я пытаюсь переписать Function.proptotype.bind следующим образом. Неожиданная ошибка с аргументами. Ожидаемый результат [a, b, c, d] - [1,2,3,4]. Но результат будет [{}, 2,3,4]

Function.prototype.bind = function(context){
  var self = this;
  
  //
  var context = [].shift.call(arguments), // this
      args = [].slice.call(arguments);
  //console.log(context, args)
  return function(){
    //return self.apply(context, arguments);
    return self.apply(context, [].concat.call(args, [].slice.call(arguments)))
  }
}

//test
var obj = {
  name : 'sven'
}

var func = function(a,b,c,d){
  var res = this.name + '___' ;
  //[a,b,c,d].map(item => res +=item + ' ')
  console.log([a,b,c,d]) 
  console.log(this.name)
  return res;
}.bind(obj, 1, 2);

var txt = func(3,4);

//console
document.getElementById("res").innerHTML = txt;

Вы можете проверить код в коде. введите здесь описание ссылки

...