简体   繁体   English

使用|| 合并第一个非null值

[英]Using || to coalesce first non null value

Here is a function: 这是一个函数:

function() {

    // if on wordpress site decipher if English or Français by using url path
    var lang = '';  
    var wp_path = document.location.pathname.match(/^\/(en|fr)\//)[0];

        if (wp_path == "/en/") {
        lang = "Français";}
        else if (wp_path == "/fr/") {
        lang = "English"; 
        }

    // if on ruby site decipher if English or Français by using querySelector on element
    var ruby_lang = document.querySelector('.menu--primary a[href*="lang="]').textContent.trim();

    // either lang or ruby_lang will be null, assign value to opp_language accordingly
    var opp_language = lang || ruby_lang;

return opp_language;

}

In the Chrome JS console this works if I type it line by line: 在Chrome JS控制台中,如果我逐行输入,则可以使用:

Eg on url http://www.example.com/en/ 例如网址http://www.example.com/en/

var lang = '';  
        var wp_path = document.location.pathname.match(/^\/(en|fr)\//)[0];

            if (wp_path == "/en/") {
            lang = "Français";} // yes NOT English, it's opposite
            else if (wp_path == "/fr/") {
            lang = "English"; 
            }

Returns Français as expected. 返回预期的Français。

Then, when I'm on a page without a url path containing either en or fr I know that the variable "ruby_lang" will return either English or Français. 然后,当我在没有包含en或fr的url路径的页面上时,我知道变量“ ruby​​_lang”将返回英语或Français。 I have verified this second part of the function works in the console. 我已经验证了该功能的第二部分在控制台中的工作原理。

So the 3rd part is presumably the problem. 因此,第三部分大概是问题所在。 My expectation is that at this point, either lang or ruby_lang are set to a truthy value. 我的期望是在这一点上,将lang或ruby_lang设置为真实值。 So: 所以:

// either lang or ruby_lang will be null, assign value to opp_language accordingly
        var opp_language = lang || ruby_lang;

But instead, whenever I run this function opp_language is undefined. 但是,每当我运行此函数时,opp_language都是未定义的。

Does the double bar || 双杠|| syntax behave in a different way than I understand? 语法的行为方式与我理解的方式不同?

If after this line var opp_language = lang || ruby_lang; 如果在此行之后var opp_language = lang || ruby_lang; var opp_language = lang || ruby_lang; your variable app_language is undefined this means that both lang and ruby_lang are evaluating to false. 您的变量app_language undefined这意味着langruby_lang都评估为false。 If all values in a "coalesce series" evaluate to false the expression will return the last value in the list. 如果“ coalesce系列”中的所有值都为false,则表达式将返回列表中的最后一个值。

If you go ahead and type in your console right now var a = b || c 如果继续,现在输入控制台,则var a = b || c var a = b || c where b and c are undefined and then log a you will see that it is indeed undefined . var a = b || c ,其中bcundefined ,然后登录a您将看到它确实是undefined

When an element is not found by document.querySelector() your ruby_language will be null or undefined , if you are getting undefined then it most probably is. document.querySelector()未找到元素时,您的ruby_language将为nullundefined ,如果得到undefined则很有可能是。

So basically make sure your variables are initialized and have default values that don't evaluate to false, I guess. 因此,基本上可以确保您的变量已初始化并且具有默认值,这些默认值不会计算为false。

I got this working but I don't really understand it. 我已经开始工作了,但我不太了解。 I used try and catch which I have never used before. 我使用了尝试捕捉功能,这是我以前从未使用过的功能。 From what I understood, if the variables could not be set they would just be falsy no? 据我了解,如果无法设置变量,那将是虚假的否?

Anyway, this works: 无论如何,这可行:

function() {

    // if on wordpress site decipher if English or Français by using url path
    var lang = '';  

  try {
    var wp_path = document.location.pathname.match(/^\/(en|fr)\//)[0];

        if (wp_path == "/en/") {
        lang = "Français";}
        else if (wp_path == "/fr/") {
        lang = "English"; 
        }
  } catch (e) {
    lang = false;
  }


  // if on ruby site decipher if English or Français by using querySelector on element

  try {
  var ruby_lang = document.querySelector('.menu--primary a[href*="lang="]').textContent.trim();
  }
  catch (e) {
    var ruby_lang = false;
  }

    // either lang or ruby_lang will be null, assign value to opp_language accordingly
    var opp_language = lang || ruby_lang;

return opp_language;

}

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

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