javascript – 新的操作符

是否可以编写一个遵循此(有效)typescript接口的javascript函数:

interface Foo{
    // constructor: 
    new (): string; 
}

即使用new运算符调用时返回字符串的东西.例如以下方法无效.

function foo(){
    return "something";
} 
var x = new foo(); 
// x is now foo (and not string) whether you like it or not :) 

解决方法:

你应该能够做到:

function foo(){
    return new String("something");
} 
var x = new foo(); 

console.log(x);

您可以返回任何对象,但文字不起作用.见:What values can a constructor return to avoid returning this?

相关文章

我最大的一个关于TypeScript的问题是,它将原型的所有方法(无...
我对React很新,我正在尝试理解子组件之间相互通信的简洁方法...
我有一个非常简单的表单,我将用户电子邮件存储在组件的状态,...
我发现接口非常有用,但由于内存问题我需要开始优化我的应用程...
我得到了一个json响应并将其存储在mongodb中,但是我不需要的...
我试图使用loadsh从以下数组中获取唯一类别,[{"listing...