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

[译] Learning Advanced Javascript #20

Open
zhouhaibing089 opened this issue Dec 11, 2016 · 0 comments
Open

[译] Learning Advanced Javascript #20

zhouhaibing089 opened this issue Dec 11, 2016 · 0 comments

Comments

@zhouhaibing089
Copy link
Owner

zhouhaibing089 commented Dec 11, 2016

original post: http://ejohn.org/apps/learn/

目标

我希望大家在读完本文之后, 可以理解下面这段代码.

// The .bind method from Prototype.js
Function.prototype.bind = function() {
    var fn = this,
        args = Array.prototype.slice.call(arguments),
        object = args.shift()
    return function () {
        return fn.apply(object,
            args.concat(Array.prototype.slice.call(arguments)))
    }
}

如果读者不太了解bind的用途的话, 可以略微参考下面的一个例子.

var F1 = function () {
  return this.first + ' ' + this.last
}
var obj = {
  first: 'FirstName',
  last: 'LastName'
}
var F2 = F1.bind(obj)
console.log(F2())

所以bind实际上是对一个函数的再一次封装, 并且指定该函数被调用时的this.

定义函数

  1. 定义函数的方式

    function isNimble () {
      return true
    }
    var canFly = function () {
      return true
    }
    window.isDeadly = function () {
      return true
    }
  2. 函数定义的顺序会有影响吗?

    var canFly = function () {
      return true
    }
    window.isDeadly = function () {
      return true
    }
    console.log(isNimble()) // true
    function isNimble () {
      return true
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant