假设我有下一个功能:
function cast<T>(v: any): T {
return v as T;
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个我想使用 map 投射的数组:
const arr = [1, 3, 5];
// works
arr.map(value => cast<number>(value));
// but I want
arr.map(cast<number>);
Run Code Online (Sandbox Code Playgroud)
是否可以?我可以在无点地图上通过通用吗?
functional-programming pointfree typescript typescript-generics
我有一个带有方法的类
function MyClass(name){
this._name = name;
}
MyClass.prototype.test=function(){
console.log(this._name);
}Run Code Online (Sandbox Code Playgroud)
如果创建新实例,则此工作
var a = new MyClass('demo');
a.test();Run Code Online (Sandbox Code Playgroud)
但是,现在我想像函数一样调用这个类而不创建实例
MyClass('demo2').test();Run Code Online (Sandbox Code Playgroud)
这可能吗?