下划线:源代码注释中的'context'是什么意思
underscore: what's meaning of 'context' in the source code comment
我对下面的代码感到困惑;评论中的'context'是什么意思?
它用于范围吗?
// If the value of the named `property` is a function then invoke it with the
// `object` as context; otherwise, return it.
_.result = function(object, property) {
if (object == null) return void 0;
var value = object[property];
return _.isFunction(value) ? object[property]() : value;
};
一个函数调用的"context"就是给this
的值。在您发布的代码中,该函数是这样调用的:
object[property]()
属性 值是从 object
中获取的,因此函数调用规则规定 object
应该是 this
的值。如果您发布的代码看起来像这样:
return _.isFunction(value) ? value() : value;
那么函数调用会有所不同:this
的值将是全局对象(window
在浏览器中),或者 undefined
如果 运行 在 "strict" 模式下。
我对下面的代码感到困惑;评论中的'context'是什么意思? 它用于范围吗?
// If the value of the named `property` is a function then invoke it with the
// `object` as context; otherwise, return it.
_.result = function(object, property) {
if (object == null) return void 0;
var value = object[property];
return _.isFunction(value) ? object[property]() : value;
};
一个函数调用的"context"就是给this
的值。在您发布的代码中,该函数是这样调用的:
object[property]()
属性 值是从 object
中获取的,因此函数调用规则规定 object
应该是 this
的值。如果您发布的代码看起来像这样:
return _.isFunction(value) ? value() : value;
那么函数调用会有所不同:this
的值将是全局对象(window
在浏览器中),或者 undefined
如果 运行 在 "strict" 模式下。