检查 javascript 中的零参数和空白字符串

发布于 2025-01-03 02:16:34 字数 399 浏览 4 评论 0原文

这是我的代码:

   JustAName.prototype = {
    statusColoring: function(idx){
     if (idx != ""){
       alert ("1st alert");
     }else{
       alert ("2nd alert");
     }
   }

当我使用 statusColoring(0) 调用 statusColoring 函数时,为什么我没有收到第一个警报?但是当我使用 statusColoring(1) 调用时,我收到第二个警报?

将零参数传递给函数并检查它的正确方法是什么?或者如何检查零/空白参数?

我已经使用 idx.length 但仍然不能..

谢谢:)

This is my code:

   JustAName.prototype = {
    statusColoring: function(idx){
     if (idx != ""){
       alert ("1st alert");
     }else{
       alert ("2nd alert");
     }
   }

when I call statusColoring function using statusColoring(0), why I don't get 1st alert? but when I call using statusColoring(1) I get 2nd alert?

how is the correct way to pass zero parameter to a function and check it? or how to check the zero/blank parameter?

I have done using idx.length but still can not..

thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倦话 2025-01-10 02:16:34

试试这个。

   statusColoring: function(idx){
     if (idx !== ""){
       alert ("1st alert");
     }else{
       alert ("2nd alert");
     }
   }

JavaScript 具有严格相等比较和类型转换相等比较。
为了严格相等,被比较的对象必须具有相同的类型
和:

  • 当两个字符串具有相同的序列时,它们严格相等
    字符,长度相同,对应的字符相同
    职位。
  • 当两个数在数值上相等时,它们严格相等(有
    相同的数值)。 NaN 不等于任何东西,包括 NaN。
    正零和负零彼此相等。
  • 如果两个布尔操作数都为 true 或都为 true,则两个布尔操作数严格相等
    错误的。
  • 如果两个对象引用同一个对象,则它们严格相等。
  • Null 和未定义类型是 == (但不是 ===)。

Try this.

   statusColoring: function(idx){
     if (idx !== ""){
       alert ("1st alert");
     }else{
       alert ("2nd alert");
     }
   }

JavaScript has both strict and type-converting equality comparison.
For strict equality the objects being compared must have the same type
and:

  • Two strings are strictly equal when they have the same sequence of
    characters, same length, and same characters in corresponding
    positions.
  • Two numbers are strictly equal when they are numerically equal (have
    the same number value). NaN is not equal to anything, including NaN.
    Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are
    false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===).
っ〆星空下的拥抱 2025-01-10 02:16:34

因为 0 != "" 返回 false

我认为你应该这样做:

 if (idx) {
   alert ("1st alert");
 } else {
   alert ("2nd alert");
 }

Because 0 != "" returns false.

I think you should just do with:

 if (idx) {
   alert ("1st alert");
 } else {
   alert ("2nd alert");
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文