zoukankan      html  css  js  c++  java
  • [Javascript] Ternary Conditionals

    /**
        Ternary Conditionals
    */
    //
    //**Bad**
    //
    var isArthur = false;
    var weapon;
    if(isArthur){
        weapon = "Excalibur";
    }else{
        weapon = "Longsword";
    }
    //
    //**Good**
    //
    var weapon = isArthur ? "Excalibur" : "Longsword";
    //
    //**Be careful**
    //
    console.log("Current weapon: "+ isArthur ? "Excalibur" : "Longsword");//This will log out "Excalibur"
    /*Why? we set isArthur = false;
      Because propriety level '+' > '?', and in Javascript only undefined, null, false, 0, NaN
      that we consider are false, other always evaluate as "truthy".
      So "String" ? "Excalibur" : "Longsword" --> "Excalibur"*/
    //
    //**To Fix**
    //
    console.log("Current weapon: "+ (isArthur ? "Excalibur" : "Longsword")); //--> "Longsword"
    
    /**
        Compound ternary conditionals
    */
    var isArthur = true;
    var isKing = false;
    console.log("Current weapon: "+ (isArthur && isKing ? "Excalibur" : "Longsword")); //--> "Longsword"
    
    /**
        Ternaries can take action
    */
    console.log(isArthur && isKing ? alert("Hail Arthur, King of the Britons!") :
                                     alert("Charge on, ye Knight, for the glory of the King!")); //--> "Charge on, ye Knight, for the glory of the King!"
    
    /**
        Build and choose functions on the fly
    */
    console.log(isArthur && isKing ? function(){
                                        alert("Hail Arthur, King of the Britons!");
                                    }() :
                                    function(){
                                        alert("Charge on, ye Knight, for the glory of the King!")
                                    }());
    //
    //**Be careful**
    //
    /*
    Remember that adding the parentheses calls the function expression!
    */
    
    /**
    Multiple actions in ternaries
    */
    console.log(isArthur && isKing ? (weapon = "Excalibur" , helmet = "Goosewhite"):
                                     (weapon = "Longsword" , helmet = "Iron Helm"));
                                     
    /**
    Lastly, ternaries can be nested!
    */
    var isArthur = true;
    var isKing = false;
    var isArcher = true;
    var weapon;
    var helmet;
    isArthur && isKing ? (weapon = "Excalibur", helmet="Goosewhite")
                        :
                        isArcher ? (weapon = "Longbos", helmet="Mail Helm")
                                 : (weapon = "Longsword" , helmet = "Iron Helm"); // --> weapon = "Longbos", helmet="Mail Helm"
    //
    //**NOTICE**: This is not easy to read! Don't do this!
    //
  • 相关阅读:
    As3支持的Html标签一览
    SharpPcap 3.4使用范例
    十六章:构建自定义集合(Part 2)
    十七章:反射、特性和动态编程(Part 1)
    十六章:构建自定义集合(Part 1)
    Implementing Finalize and Dispose to Clean Up Unmanaged Resources
    HDU1711Number Sequence(KMP)
    HDU2087剪花布条(KMP)
    HDU1671Phone List (trie树)
    HDU 1075 What are you talking about(trie树)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3903858.html
Copyright © 2011-2022 走看看