我有一个名为 obj
的对象,它有一个嵌套对象 comments
和一个返回如下对象的函数 startMatch
:
var obj = {};
obj.comments = {
startMatch: function(matchStrings, isCaseSensitive) {
return {
subscribe: function(delegate) {
delegate('test')
const unsubscribe = () => {
console.log("unsubscribed");
};
}
};
}
};
var subscription = obj.comments.startMatch([], false).subscribe(function(result) {
console.log(result)
});
我想用这样的方式来调用 unsubscribe
函数:
subscription.unsubscribe();
但我无法弄清楚如何在不收到取消订阅未定义错误的情况下执行此操作。
最佳答案
选择最简单的方式:
var obj = {};
obj.comments = {
startMatch: function(matchStrings, isCaseSensitive) {
return {
subscribe: function(delegate) {
delegate('test');
return { unsubscribe: () => console.log("unsubscribed") }
}
};
}
};
var subscription = obj.comments.startMatch([], false).subscribe(function(result) {
console.log(result)
});
subscription.unsubscribe();
关于javascript - 如何从嵌套对象调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57649525/