1. 程式人生 > >格式化字符串(三)

格式化字符串(三)

comm “.” ack ref github 例如 console 排列組合 href

格式化字符串(三)

直接使用字符串“.”方法的方式格式化字符串。

字符串增加judge方法,使用判斷的方式格式化字符串。


String.judge({JSON},|Boolean|?)

  1. 入門用法。

    例如:

     var string = '問:v2是一個優秀的前端框架麽?答:{if(isGood){ "是" } else { "否" }^}。'.judge({ isGood: true });
     console.log(string); // => 問:v2是一個優秀的前端框架麽?答:是。

    說明:
    用法{if(|BooleanExpresion|) { TrueString } else { FalseString }^}

    1的方式嵌入字符串中。

  2. 初級用法。

    例如:

     var string = '問:v2是一個優秀的前端框架麽?答:{if(isGood){ "是" } else if(isCommon) { "一般" } else { "否" }^}。'.judge({ isGood: false, isCommon: true });
     console.log(string); // => 問:v2是一個優秀的前端框架麽?答:一般。

    說明:
    用法{if(|BooleanExpresion|){ TrueString } else if(|BooleanExpresion|) { ElseIfString } else { FalseString }^}

    2的方式嵌入字符串中。

  3. 中級用法。

    例如:

     var string = '問:v2是一個優秀的前端框架麽?答:{if(isGood){ ^"appraise+"(票數:"+ votes * platform +")"" } else if(isCommon) { "一般" } else { "否" }^}。'.judge({ isGood: true, isCommon: true, appraise: "非常棒的", votes: 1024, platform: 5 });
     console.log(string); // => 問:v2是一個優秀的前端框架麽?答:非常棒的(票數:5120)。

    說明:
    用法{if(|BooleanExpresion|){ ^TrueString } else if(|BooleanExpresion|) { ^ElseIfString } else { ^FalseString }^}3的方式嵌入字符串中。

  4. 高級用法。

    例如:

     var string = '問:v2是一個優秀的前端框架麽?答:{if(isGood){ $"是{appraise}" } else if(isCommon) { "一般" } else { "否" }^}。'.judge({ isGood: true, isCommon: true, appraise: "非常棒的" });
     console.log(string); // => 問:v2是一個優秀的前端框架麽?答:是非常棒的。

    說明:
    用法{if(|BooleanExpresion|){ $TrueString } else if(|BooleanExpresion|) { $ElseIfString } else { $FalseString }^}4的方式嵌入字符串中。

  5. 進價級用法。

    說明:
    以上四種方式可有排列組合執行。

參考代碼:Github


  1. {if(|BooleanExpresion|){ TrueString }else{ FalseString }^}BooleanExpresion 判斷的表達式,TrueString單表達式為真的返回值,否則返回FalseString(Else條件可有可無)。?

  2. {if(|BooleanExpresion|){ TrueString } else if(|BooleanExpresion|) { ElseIfString } else { FalseString }^}BooleanExpresion 判斷的表達式,TrueString單表達式為真的返回值,否則依次進入下一個ElseIf判斷,為真時返回ElseIfString,都不滿足時返回FalseString(可以有多個ElseIf條件)。?

  3. {if(|BooleanExpresion|){ ^TrueString } else if(|BooleanExpresion|) { ^ElseIfString } else { ^FalseString }^}^標記返回值時,返回值將被作為表達式運算(相當於String.replace({JSON},|Boolean|?)提取的單個表達式)。?

  4. {if(|BooleanExpresion|){ $TrueString } else if(|BooleanExpresion|) { $ElseIfString } else { $FalseString }^}$標記返回值時,返回值將執行String.replace({JSON},|Boolean|?)方法。(查看使用詳情:Blog)?

格式化字符串(三)