vue.js methods中的方法互相调用时变量的作用域是怎样的

如题所述

methods中的function中的this指向vue实例,其他的没什么这种调用方式是直接访问test2函数,没有任何的this绑定,所以肯定访问不到

this.$options.methods.test2();

而直接调用this.test2(),内部肯定做了this绑定的,例如

this.$options.methods.test2.bind(this)();

更新:Vue源码中的处理

/**
* Setup instance methods. Methods must be bound to the
* instance since they might be passed down as a prop to
* child components.
*/
Vue.prototype._initMethods = function () {
var methods = this.$options.methods
if (methods) {
for (var key in methods) {
this[key] = bind(methods[key], this)
}
}
}

function bind (fn, ctx) {
return function (a) {
var l = arguments.length
return l
? l > 1
? fn.apply(ctx, arguments)
: fn.call(ctx, a)
: fn.call(ctx)
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-08-04
methods中的function中的this指向vue实例,其他的没什么这种调用方式是直接访问test2函数,没有任何的this绑定,所以肯定访问不到
第2个回答  2017-08-04
methods中的function中的this指向vue实例,其他的没什么这种调用方式是直接访问test2函数,没有任何的this绑定,所以肯定访问不到

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网