我有以下功能
function hello() {
alert("hi!");
}
Run Code Online (Sandbox Code Playgroud)
拿这段代码:
var elem = document.getElementById("btn");
elem.onclick = hello;
Run Code Online (Sandbox Code Playgroud)
我的问题可能有点难以理解,所以请耐心看看:这段代码与普通调用有什么区别,或者是什么使得这段代码需要引用函数变量而不是常规调用?(hello();)
我怎么知道我应该在哪里提供函数的引用,以及什么时候我应该实际调用它?
最近我需要使用 RxJS。我试图设计一个错误处理流程,但我发现了一些奇怪的语法传递方法参数:
.subscribe(
x => {
},
console.warn // <- Why does this compile, and warn 'is not 7' in debug console?
);
Run Code Online (Sandbox Code Playgroud)
https://stackblitz.com/edit/rxjs-6-5-error-handle-no-arrow-issue
,console.warn,不喜欢,error => { console.warn(error); }如果没有箭头函数,它仍然会将错误传递给 console.warn。为什么?
代码:
import { throwError, concat, of } from "rxjs";
import { map } from "rxjs/operators";
const result = concat(of(7), of(8));
getData(result).subscribe(
x => {
console.log("typeof(x)", typeof(x));
if (typeof(x) === 'string') {
console.log("x Error", x);
return;
}
console.log("no …Run Code Online (Sandbox Code Playgroud) 我们使用()来调用函数,但我很困惑为什么我们在javascript中的onclick事件中附加函数时只使用函数名.
function a(){
alert(0)
}
document.getElementById('btn').onclick=a
<input value="click" type="button" id="btn" />
Run Code Online (Sandbox Code Playgroud)