這會導致以下錯誤:Cannot read property 'length' of undefinedconst msg$ = new Subject<string>();msg$.subscribe(console.log)of("Hello").subscribe(msg$.next);但是,如果我將 msg$.next 包裝在函數中,則它可以正常工作而不會出現任何錯誤。拉姆達函數const msg$ = new Subject<string>();msg$.subscribe(console.log)of("Hello").subscribe(greeting => msg$.next(greeting));匿名函數const msg$ = new Subject<string>();msg$.subscribe(console.log)of("Hello").subscribe(function(greeting){ msg$.next(greeting);});命名函數function nextMsg(greeting){ msg$.next(greeting);}const msg$ = new Subject<string>();msg$.subscribe(console.log)of("Hello").subscribe(nextMsg);它們都只是包裝函數,看起來除了調用下一個函數之外什么也不做。這里發生了什么?似乎這里有一個我不知道的 JavaScript 陷阱。
對 next 的調用導致奇怪的錯誤
呼啦一陣風
2023-09-21 17:23:19