更新时间:2021-12-31 22:59:30
使用括号,因为括号和结果被调用该调用的em>将存储在before_add中.
With parentheses the method is invoked because of the parentheses, and the result of that invocation will be stored in before_add.
不包含的括号中,您存储了对该变量中函数的引用(如果可以,则为指针").这样,只要有人调用before_add(),它就会被调用.
Without the parentheses you store a reference (or "pointer" if you will) to the function in the variable. That way it will be invoked whenever someone invokes before_add().
如果这不能解决问题,也许这会有所帮助:
If that didn't clear things up, maybe this will help:
function Foo() {
return 'Cool!';
}
function Bar(arg) {
console.log(arg);
}
// Store the >>result of the invocation of the Foo function<< into X
var x = Foo();
console.log(x);
// Store >>a reference to the Bar function<< in y
var y = Bar;
// Invoke the referenced method
y('Woah!');
// Also, show what y is:
console.log(y);
// Now, try Bar **with** parentheses:
var z = Bar('Whut?');
// By now, 'Whut?' as already been output to the console; the below line will
// return undefined because the invocation of Bar() didn't return anything.
console.log(z);
如果您随后查看浏览器的控制台窗口,应该会看到:
If you then take a look at your browsers' console window you should see:
Cool!
Woah!
function Bar(arg)
Whut?
undefined
第1行是调用 Foo()
,
的结果第2行是调用 Bar()
通过" y
,
第3行是 y
,
的内容"第4行是 var z = Bar('Whut?');
行的结果;Bar功能被调用,
第5行显示调用 Bar()
并将结果分配给 z
不会返回任何内容(因此: undefined ).
Line 1 is the result of invoking Foo()
,
Line 2 is the result of invoking Bar()
"via" y
,
Line 3 is the "contents" of y
,
Line 4 is the result of the var z = Bar('Whut?');
line; the Bar function is invoked,
Line 5 shows that invoking Bar()
and assigning the result to z
didn't return anything (thus: undefined).