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?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐