.call(this)在JavaScript中的成语
在一些JavaScript中,我看到:
var passport = require('passport') function Strategy(options, verify) { //... passport.Strategy.call(this); }
什么是passport.Strategy.call(this);
在做什么?
在该代码的上下文中,这实际上是一种执行超级构造函数的方法。 例如,查看护照本地对象,护照本地类的原型inheritance自策略原型。 有效的是护照的“小类”。策略。 当你创build一个新的passport.Local实例时,你也会想要执行超级构造函数(passport.Strategy)。 做constructor.call(context)
允许你在子类的上下文中执行超级构造器。
.call()
函数是一个原生的JavaScript函数,它将上下文传递给函数。 在这种情况下,调用函数passport.Strategy()
并传递它的上下文。
这意味着在passport.Strategy()
函数内, this
对象引用了第一个传递给.call()
函数的variables。