使用打字稿...
public subscribe:(subscribeFunction:(state)=>void)=>()=>void;
我理解这是对的吗? 它是一个订阅方法,它将函数作为参数,函数类型和给定的函数,在调用时接收状态参数,争论函数不会返回任何东西(即:void)......我迷失了在last()=>()=> void
上:/?
肖恩
答案 0 :(得分:8)
这是一个名为subscribe
的公共财产,其类型为(subscribeFunction: (state) => void) => () => void;
:
// scope
public
// name
subscribe:
// type (function)
// parameters
(
// parameter name
subscribeFunction:
// parameter type (function)
(state) => void
) =>
// return type (function)
() => void;
这是一个编译的例子:
class MyClass {
public subscribe: (subscribeFunction: (state) => void) => () => void;
}
let myInstance = new MyClass();
myInstance.subscribe = (subscribeFunction: (state) => void) => {
console.log("statements might go here");
return () => {
subscribeFunction(1 /* state */);
console.log("nothing returned by this inner function");
};
};
// Example use
// outputs "statements might go here"
let innerFunction = myInstance.subscribe((state) => console.log(state));
// outputs 1 and "nothing returned by this inner function"
innerFunction();
答案 1 :(得分:0)
注意第一个冒号 - subscribe
是函数类型的公共属性,而不是方法。展开:
public subscribe: // public property, which is
( // of the type of a function, which takes
subscribeFunction: // 1 parameter, which itself is
(state) => void // a function of 1 parameter, returning nothing
) => // and the upon invocation of a function stored in "subscribe" it
() => void; // returns a function of no parameter and no return value
因此,你可以将一个函数存储到subscribe
属性中,然后在给它一个函数作为参数时调用它,结果你将获得另一个函数,你可以随后调用它:
subscribe = (subscribeFunction: (state) => void) => {
subscribeFunction('A');
return () => {console.log('C');};
};
let subscribed = subscribe((state) => {
console.log(state, 'B');
}); // prints 'A,B'
subscribed(); // prints 'C'
答案 2 :(得分:0)
获取反馈意见,现在据我所知,我认为最好解释为:
public subscribe:(subscribeFunction:(state)=> void)
//第一个药水用于subscribeFunction,它将接受一个不返回任何内容的函数
=>()=>无效;
//第二个药水用于订阅本身,因为它将返回一个不返回任何内容的函数......我同意,这样的代码应该更明确