JavaScript實現儲存HTML字串

來源:互聯網
上載者:User

吃完晚飯,在翻各位大牛的部落格,偶然看到一篇原創《原創--Javascript你意想不到的功能!!!》眼前一亮,
這思路確實霸氣測漏,可以不用理會字串的單引號和雙引號的轉義,因為人家用的是注釋,注釋當然什麼都可以寫。
不過原文寫的略顯羞澀,我來修改個版本,就當是日記記下、

PS:我是搞PHP的,讓我想起了<<<文法(heredoc和nowdoc),那麼就為他命名heredoc吧。

 1 Function.prototype.heredoc = function(){ 2     // 利用 function 的注釋來儲存字串,而且無需轉義。 3     var _str = this.toString(), 4         s_pos = _str.indexOf("/*")+2, 5         e_pos = _str.lastIndexOf("*/"); 6     return (s_pos<0 || e_pos<0) ? "" : _str.substring(s_pos, e_pos); 7 } 8  9 function fn(){10 /*<table>11     <tr>12         <td>使用者名稱</td>13         <td>密碼</td>14     </tr>15     <tr>16         <td style="widht:20px;">@name</td>17         <td>zf123456</td>18     </tr>19 </table>*/20 }21 22 var str_table = fn.heredoc();23 console.log(str_table);

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.