this.function 和 this.function() 之间的区别
Difference between this.function and this.function()
这样做有什么区别.functionName vs this.functionName()?我之间没有一个明确的理解。
this.functionName
读取对象this
的属性functionName
的值。
this.functionName()
读取对象this
的属性functionName
的值,并尝试将其作为函数调用。如果 this.functionName
的值不是函数,它将引发错误。
将语句分解为
this.functionName()
'____callee_____/
'_CallExpression__/
"被调用方"可以是任何表达式,但必须解析为函数对象。
如果您好奇,请查看 AST。
计算时,第一个只是对函数的引用,在第二种情况下,函数被执行,表达式将被计算为函数的返回值。
this.functionName
是对函数对象的引用。
如果您想在调用函数之前检查函数是否存在,这会很有用,例如
if (typeof(this.functionname) == 'function') {
this.functionname();
}
this.functionName()
实际上调用了该函数。
this.functionName 返回函数本身,this.functionName() 将执行函数并返回结果
function A() {
this.doSomething = function() {
return "A";
}
this.doOtherThing = function() {
console.log(this.doSomething);
console.log(this.doSomething());
}
}
var a = new A();
a.doOtherThing()
将以下内容打印到控制台:
function () {
return "A";
}
A
第一个是函数本身,第二个是函数的结果。您可能希望返回函数而不执行它的原因有很多,例如,可以将其传递给另一个函数,以便在 AJAX 请求完成后调用。
相关文章:
- this内部的值返回this.each(function(){})
- This.props.dispatch not a function - React-Redux
- jQuery $(this) inside function
- 重复“;function(){}.调用(this)"在Uglified JS中
- Javascript get "this" class with onClick function
- 用(function(){…}).call(this)包装文件,而用简单的()包装调用
- Function.length和this.length不一样.为什么
- jQuery: addClass and click function for this class
- 函数(){}.bind(this) 和 angular.bind(this, function() {}) 之间的区别
- Advantage of (function() {/*...*/}).call(this);
- this.function 和 this.function() 之间的区别
- 为什么使用 (function(){}).call(this);.
- this.function不是函数错误,但函数存在
- TypeError:调用Function.protype.method()时,this.prototype未定义
- undefined is not a function when evaluating 'this.batman
- Javascript setTimeout function with 'this'
- this.text Function
- 如何理解“{ fun: function.bind(this) }”
- 我如何在this.function.bind(this)中替换任何数值作为参数
- 为什么是全球性的"this.function";如果没有";这个"