简体   繁体   English

如果带有typeof检查的步步返回未定义

[英]if else stament with the typeof check is returning undefined

updated 更新

hey I tested your code...its breaking for another scenario if I give mobileVersion as sample.pdf its breaking...I am getting skyCloudmageProfilePic as pdfProfilePic...this is without remove typeOf jsfiddle.net/n9d08ko2 嘿,我测试了您的代码...如果我给mobileVersion作为sample.pdf,它会破坏另一种情况...我正在获取skyCloudmageProfilePic作为pdfProfilePic ...这是无法删除typeOf jsfiddle.net/n9d08ko2

where as if I use your conditionI am getting skyCloudmageProfilePic as newDocProfilePic how to get as pdfProfilePic... providing your updated code in this fiddle jsfiddle.net/Lqjhyvmz 好像我在使用您的条件,我正在将skyCloudmageProfilePic作为newDocProfilePic,如何获取pdfProfilePic ...在此小提琴中提供您的更新代码jsfiddle.net/Lqjhyvmz

if (model.mobilePics) {
    skyCloudmageProfilePic = model.mobilePics;
 } else {
    skyCloudmageProfilePic = "newDocProfilePic";
 }
  • I am trying to remove typeof from my if condition. 我正在尝试从if条件中删除typeof。
  • if I remove typeof I am getting undefined for skyCloudmageProfilePic. 如果删除typeof,则无法获取skyCloudmageProfilePic。
  • is there any way to fix it. 有什么办法可以解决。
  • providing my code below. 在下面提供我的代码。
  • the reson I am trying to remove typeOf is I need to fix the lint error. 我试图删除typeOf的原因是我需要修复棉绒错误。
  • since typeof is not accepted. 因为typeof不被接受。
  • can you tell me how to fix it provding my code below 你能告诉我如何解决它吗?

  • I was debugging in this fiddle when it reaches this line let kendotxtMenu = ""; 当到达此行时,我正在调试这个小提琴,让kendotxtMenu =“”; you can see the value for skyCloudmageProfilePic as newDocProfilePic skyCloudmageProfilePic = newDocProfilePic 您可以将skyCloudmageProfilePic的值查看为newDocProfilePic skyCloudmageProfilePic = newDocProfilePic

http://jsfiddle.net/fdnoz2ka/ http://jsfiddle.net/fdnoz2ka/

  • but in this fiddle if I remove typeof, you can see the value for skyCloudmageProfilePic as undefined skyCloudmageProfilePic = undefined 但是在这种提琴演奏中,如果我删除typeof,您可以将skyCloudmageProfilePic的值视为undefined skyCloudmageProfilePic = undefined

http://jsfiddle.net/c4k6mqkg/ http://jsfiddle.net/c4k6mqkg/

//if (typeof model.mobilePics != "undefined" && model.mobilePics != "") {
          //skyCloudmageProfilePic = model.mobilePics; // skyCloudmageProfilePic = newDocProfilePic
       // }

       //if (typeof skyCloudmageProfilePic == "undefined") {
         // skyCloudmageProfilePic = "newDocProfilePic"; // skyCloudmageProfilePic = newDocProfilePic
       // }

        if ( model.mobilePics != "undefined" && model.mobilePics != "") {
          skyCloudmageProfilePic = model.mobilePics; // skyCloudmageProfilePic = undefined
        }

        if (skyCloudmageProfilePic == "undefined") {
          skyCloudmageProfilePic = "newDocProfilePic"; // skyCloudmageProfilePic = undefined
        }

        let kendotxtMenu = "";
 if (model.mobilePics) {
    skyCloudmageProfilePic = model.mobilePics;
 } else {
    skyCloudmageProfilePic = "newDocProfilePic";
 }

This should work, in the first you check for undefined and an empty string, but a string will always evaluate to false if it is either undefined or empty. 这应该可以工作,首先检查未定义的字符串和空字符串,但是如果字符串未定义或为空,则字符串的总值为false。

After looking a little harder I realized your second check is pointless as well. 经过一番努力后,我意识到您的第二张支票也毫无意义。 I've adjusted it to fit better. 我已经对其进行了调整以使其更适合。

You should also use '===' wherever possible, this avoids equality checks doing funky things to you. 您还应尽可能使用“ ===”,这样可以避免进行相等性检查,以免对您造成麻烦。 Here's a little post that explains the triple check. 这是一篇介绍三重检查的小文章。

Which equals operator (== vs ===) should be used in JavaScript comparisons? JavaScript比较中应使用哪个等于运算符(== vs ===)?

Use the Boolean value of the variable to check for defined/undefined state: 使用变量的布尔值检查定义/未定义状态:

 var x = 1; var y = undefined; if (x) { // if defined console.log("I'm defined"); } if (!y) { // if undefined console.log("I'm undefined"); } 

Or, compare the variables with the type undefined instead of the string "undefined" : 或者,将变量与undefined类型而不是字符串"undefined"

 var x = 1; var y = undefined; if (x !== undefined) { // if defined console.log("I'm defined"); } if (y === undefined) { // if undefined console.log("I'm undefined"); } 

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

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