相关疑难解决方法(0)

函数调用和函数引用有什么区别?

我有以下功能

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();)

我怎么知道我应该在哪里提供函数的引用,以及什么时候我应该实际调用它?

javascript reference function call

24
推荐指数
3
解决办法
6144
查看次数

为什么 RxJS 订阅允许省略箭头函数和以下方法参数?

最近我需要使用 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

重现步骤:

  1. 使用 RxJS 6.5
  2. 创建一个函数返回 observable
  3. 订阅可观察的
  4. 将参数传递给订阅
  5. 只是使用,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 operator-keyword rxjs angular

5
推荐指数
3
解决办法
687
查看次数

在JavaScript中调用没有括号的函数

我们使用()来调用函数,但我很困惑为什么我们在javascript中的onclick事件中附加函数时只使用函数名.

function a(){
alert(0)
}

document.getElementById('btn').onclick=a


<input value="click" type="button" id="btn" />
Run Code Online (Sandbox Code Playgroud)

javascript

2
推荐指数
2
解决办法
1732
查看次数