Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Coffeescript 检查字符串是否为null或空的最简单方法_Coffeescript - Fatal编程技术网

Coffeescript 检查字符串是否为null或空的最简单方法

Coffeescript 检查字符串是否为null或空的最简单方法,coffeescript,Coffeescript,我有一个检查空字符串或空字符串的代码。它在测试中起作用 eitherStringEmpty= (email, password) -> emailEmpty = not email? or email is '' passwordEmpty = not password? or password is '' eitherEmpty = emailEmpty || passwordEmpty test1 = eitherStringEmpty "A", "B"

我有一个检查空字符串或空字符串的代码。它在测试中起作用

eitherStringEmpty= (email, password) ->
  emailEmpty = not email? or email is ''
  passwordEmpty = not password? or password is ''
  eitherEmpty = emailEmpty || passwordEmpty         

test1 = eitherStringEmpty "A", "B" # expect false
test2 = eitherStringEmpty "", "b" # expect true
test3 = eitherStringEmpty "", "" # expect true
alert "test1: #{test1} test2: #{test2} test3: #{test3}"
我想知道的是,是否有比不发电子邮件更好的方法?或电子邮件是“”。我可以用一个调用在CoffeeScript中执行与C#
string.IsNullOrEmpty(arg)
等效的操作吗?我总是可以为它定义一个函数(就像我所做的那样),但我想知道在语言中是否有我遗漏的东西

是的:

passwordNotEmpty = not not password
或更短:

passwordNotEmpty = !!password

它不是完全等效的,但只有当
电子邮件
为非空且具有非零
长度
属性时,
email?length
才是真实的。如果您
而不是
此值,则字符串和数组的结果应按您所需的方式运行

如果
email
null
或没有
.length
,则
email?.length
将计算为
null
,这是错误的。如果它确实有一个
.length
,则该值将计算为其长度,如果该值为空,则该值将为假

您的功能可以实现为:

eitherStringEmpty = (email, password) ->
  not (email?.length and password?.length)
在这种情况下,“真实性”就派上了用场。您甚至不需要为此定义函数:

test1 = not (email and password)
它为什么有效

'0'       // true
'123abc'  // true
''        // false
null      // false
undefined // false

如果需要检查内容是否为字符串、非null和非数组,请使用简单的比较类型:

 if typeof email isnt "string"
下面是一个演示一种非常简单的方法

基本上,您只需使用javascript即可:

var email="oranste";
var password="i";

if(!(email && password)){
    alert("One or both not set");        
}
else{
    alert("Both set");   
}
if (car != null) {
  car.color;
}
在coffescript中:

email = "oranste"
password = "i"
unless email and password
  alert "One or both not set"
else
  alert "Both set"

希望这对某人有所帮助:)

我认为问号是对存在的对象调用函数的最简单方法

比如说

car = {
  tires: 4,
  color: 'blue' 
}
你想要得到颜色,但前提是汽车存在

咖啡脚本:

 car?.color
转换为javascript:

var email="oranste";
var password="i";

if(!(email && password)){
    alert("One or both not set");        
}
else{
    alert("Both set");   
}
if (car != null) {
  car.color;
}
它被称为存在算子

首先使用存在运算符检查电子邮件是否未定义且不为空,然后如果您知道它存在,则如果电子邮件字符串为空,则
和电子邮件部分将仅返回false。

基于关于检查变量是否具有
truthy
值,您只需要一行:

result = !email or !password

&您可以在这个

上亲自尝试,而不是接受答案
密码notempty=!!密码
您可以使用

passwordNotEmpty = if password then true else false
它给出了相同的结果(只是语法上的不同)

第一列是一个值,第二列是值
if value
的结果:

0 - false
5 - true
'string' - true
'' - false
[1, 2, 3] - true
[] - true
true - true
false - false
null - false
undefined - false

您可以使用coffeescript或=操作

s = ''    
s or= null

我很确定@thejh的答案足够好,可以检查空字符串,但是, 我认为我们经常需要检查“它存在吗?”然后我们需要检查“它是空的吗?”?包括字符串、数组和对象的

这是CoffeeScript实现这一点的缩短方法

tmp? and !!tmp and !!Object.keys(tmp).length
如果我们保持这个问题的顺序,这将被这个顺序检查 1.它存在吗? 2.不是空字符串吗? 3.不是空对象吗


因此,即使在不存在的情况下,所有变量也没有任何问题。

这是两个答案中更为“javascript-y”的一个。特别是如果您使用
版本,这是基本上转换为布尔值的常用方法。如果有关系的话,这几乎肯定比Jeremy建议的定义函数快。它们都有效,但这一个得到了最多的支持。我喜欢可读性tho。它对所有空格的字符串都无效…所以对blank@Arkhitech空白和空白不是一回事,所以它很有效。太“聪明”了,不符合我的可读性;我想我不会用它,但干得好though@pyrotechnick这个问题没有问数组和字符串的区别。它询问如何区分空值和空字符串与非空字符串。我的观点是,代码还可以区分空值和空数组与非空数组,但这超出了问题的范围。然而,“eitherStringEmpty”对于您提供的方法是一个不正确的名称。空字符串为存在性检查返回true。
typeof email不是“string”
'
'a'
返回false。我假设您已经调用了
.trim()
,作为验证逻辑的一部分。答案更多的是关于CS语法。哇,这太神奇了!谢谢