美文网首页
编写高质量js代码(一)

编写高质量js代码(一)

作者: 陈左夕 | 来源:发表于2017-08-17 23:22 被阅读0次

使用bind方法提取具有确定接受者的方法

  • 假设一个字符串缓冲对象使用数组来存储字符串,该数组稍后可能被连接起来
var buffer = {
     entries: [],
     add: function(s) {
         this.entries.push(s);
     },
     concat: function() {
         return this.entries.join('');
     }
};
// 通过ES5的forEach方法复制字符串数组到buffer对象中
var source = ['867', '-', 5309'];
source.forEach(buffer.add);  // error: entries is undefined

相关文章

网友评论

      本文标题:编写高质量js代码(一)

      本文链接:https://www.haomeiwen.com/subject/ufhorxtx.html