About Bind in Prototype JavaScript Language.

This is the first article on JavaScript, more will be put here in Chinese maybe.
Some reference is from the Prototype website, and some from JavaScript the definitive guide 5th edition, rest is my comment. hope this helps.

/*
    “Binding” basically determines the meaning, when a function runs, of the this keyword.
    While there usually is a proper default binding (this refers to whichever object the method is called on),
    this can be “lost” sometimes, for instance when passing a function reference as an argument.
   
    the execution scope is the object from which the function was called,
    or (more precisely) the object that holds a reference to the function.
  */
  bind: function() {
    if (arguments.length < 2 && arguments[0] === undefined) return this;    // if nothing to bind in arguments
/* following has a $A function call, basically reference the current argument or copy current argument into a newly created array. which maintain the value of the arguments for the binding function ^^ */
    var __method = this, args = $A(arguments), object = args.shift();   // object set to the first element in the arguments
    /*
        The Array.concat() method creates and returns a new array that contains the elements of the original
        array on which concat() was invoked, followed by each of the arguments to concat(). If any of these
        arguments is itself an array, it is flattened, and its elements are added to the returned array. Note,
        however, that concat() does not recursively flatten arrays of arrays.
    */
    /*
        Functions are objects in JavaScript, and apply allows you to apply the method of one object
        in another object, which basically means your controlling the execution scope.
    */
    return function() {
      return __method.apply(object, args.concat($A(arguments)));    // arguments here is newly passed in arguments, not the one above!
    }
  },
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值