node / javascript () () 语法 - 它是如何工作的?
node / javascript () () syntax - how does it work exactly?
帮助我理解这段代码,of (1,2,3)
的输出如何通过管道传输到 map( x => x*x)
,尽管 map( x => x*x)
在代码行中排在第一位,而 of (1,2,3)
排在第二位
map(x => x*x) (of (1,2,3)).subscribe((value)=> console.log(`value : ${value}`))
同样可以写成下面这样,我明白了,但不能写在上面..
of(1,2,3).pipe(map(x => x*x)).subscribe((value)=> console.log(`value : ${value}`))
仅供参考,两者都是正确的,return 值 1,4,9
如果您在编辑器中尝试相同,请包含以下导入
import {of} from 'rxjs'
import {map} from 'rxjs/operators'
这实际上是 RxJS 文档中的一个例子,上面有解释:
A Pipeable Operator is essentially a pure function which takes one Observable as input and generates another Observable as output. Subscribing to the output Observable will also subscribe to the input Observable.
所以这意味着 map(x => x*x)
returns 一种函数,它将一个 Observable 作为参数,returns 另一个 Observable。然后我们用 (of(1,2,3))
调用该函数并得到我们的最终结果,实际上等于 of(1,2,3).pipe(map(x => x*x))
它更多的是 JavaScript 功能。如果您看到函数被调用为 foo()()
或 foo() ('hello')
,这意味着 foo 正在返回另一个函数,并且第二个“()”中的参数像 foo() ('hello')
中的 'hello' 被传递给函数由 foo() 返回。
例子
将下面的代码保存为 sample.js,然后使用 node sample
执行它
foo()() // return undefined, as empty parameter ( parenthesis ) are passed though expecting one
foo()('hello') // param 'hello' is passed to bar when it is returned inside foo
foo()('hello','world') //both hello and world are passed, but only hello is printed, as bar expect only 1 param
function foo() {
return bar
}
function bar (param) {
console.log('bar : '+param)
}
结果
bar : undefined
bar : hello
bar : hello
帮助我理解这段代码,of (1,2,3)
的输出如何通过管道传输到 map( x => x*x)
,尽管 map( x => x*x)
在代码行中排在第一位,而 of (1,2,3)
排在第二位
map(x => x*x) (of (1,2,3)).subscribe((value)=> console.log(`value : ${value}`))
同样可以写成下面这样,我明白了,但不能写在上面..
of(1,2,3).pipe(map(x => x*x)).subscribe((value)=> console.log(`value : ${value}`))
仅供参考,两者都是正确的,return 值 1,4,9
如果您在编辑器中尝试相同,请包含以下导入
import {of} from 'rxjs'
import {map} from 'rxjs/operators'
这实际上是 RxJS 文档中的一个例子,上面有解释:
A Pipeable Operator is essentially a pure function which takes one Observable as input and generates another Observable as output. Subscribing to the output Observable will also subscribe to the input Observable.
所以这意味着 map(x => x*x)
returns 一种函数,它将一个 Observable 作为参数,returns 另一个 Observable。然后我们用 (of(1,2,3))
调用该函数并得到我们的最终结果,实际上等于 of(1,2,3).pipe(map(x => x*x))
它更多的是 JavaScript 功能。如果您看到函数被调用为 foo()()
或 foo() ('hello')
,这意味着 foo 正在返回另一个函数,并且第二个“()”中的参数像 foo() ('hello')
中的 'hello' 被传递给函数由 foo() 返回。
例子
将下面的代码保存为 sample.js,然后使用 node sample
foo()() // return undefined, as empty parameter ( parenthesis ) are passed though expecting one
foo()('hello') // param 'hello' is passed to bar when it is returned inside foo
foo()('hello','world') //both hello and world are passed, but only hello is printed, as bar expect only 1 param
function foo() {
return bar
}
function bar (param) {
console.log('bar : '+param)
}
结果
bar : undefined
bar : hello
bar : hello