简体   繁体   English

JavaScript中的String.empty

[英]String.empty in JavaScript

In .NET, I tend to use String.Empty when I want to use an empty string. 在.NET中,当我想使用空字符串时,我倾向于使用String.Empty

I would like to add this functionality to the String object in JavaScript. 我想将此功能添加到JavaScript中的String对象。

My first thought was something like this: 我的第一个想法是这样的:

if(!String.empty){
    String.empty = '';
}

I suppose this is the right way to go? 我想这是正确的方法?

The reason that I'm asking is because I want to know if I should involve the String.prototype . 我问的原因是因为我想知道是否应该涉及String.prototype But it feels wrong since this has nothing to do with actually instantiating a new String() . 但这是错误的,因为这与实际实例化一个new String()无关。 Or am I wrong? 还是我错了?

I do always like this: 我总是喜欢这样:

var str = $.trim(' string ');
if(str){
   // not empty
 }else{
   // empty
 }

For all I know, you are completely right. 就我所知,你是完全正确的。 The String.prototype would give an 'empty' property to all your strings, but that is clearly not what you want. String.prototype将为您的所有字符串赋予“空”属性,但这显然不是您想要的。 You could even omit the 'if' part and just write 您甚至可以省略“如果”部分,然后写

String.empty = '';

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM