代碼中的美元$

來源:互聯網
上載者:User
文章目錄
  • JavaScript
  • Java
  • Regex
  • CSS
  • PHP
  • Bash Shell
  • iBatis
  • Visual FoxPro
  • 其它的其它

這個題目其實有點標題黨了,其實本篇文章主要內容應該是說“代碼中的貨幣符號(dollar sign)”。想總結一下我所看到的貨幣符號在代碼中的起到的作用,當然了並不是說它作為格式化字元時出現在數字前當錢幣單位的作用。

 有效變數名

在取變數名的時候,我們通常都只使用字母,數字,底線,而且數字不能作為開頭。但是在很多程式設計語言中,對於變數名的限定更寬泛,很多特殊字元也可以作為變數名,當然也包括$。

JavaScript

JavaScript允許$符號作為有效識別來當做變數名或者方法名來使用。最著名的例子就是jQuery了,整個使用過程中到處都是$。$只是jQuery變數的一個別名,但是也正式$的短小、高可識別度,在編寫代碼時帶來了很多的便利。

$("button").click(function () {        $("p").toggle();});

對於還能用哪些奇怪的字元作為JavaScript的變數,可以通過JavaScript variable name validator來檢驗有效性。

Java

Java其實也可以將$作為有效變數,只是它不能作為使用者定義,而是預留給編譯器使用。

Variable names are case-sensitive. A variable’s name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign “$, or the underscore character “_“. The convention, however, is to always begin your variable names with a letter, not “$” or “_“. Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it’s technically legal to begin your variable’s name with “_“, this practice is discouraged. White space is not permitted.

比如說嵌套類(nested class),在編譯過後實際的類名都是各層嵌套類以$為分隔字元“粘合”在一起。看看下邊的簡單樣本:

 package com.ider; public class Dollar {    static class Sign {         }         public static void main(String[] args) {            Dollar.Sign sign = new Dollar.Sign();            System.out.println(sign.getClass().getName());             } }

 

輸出結果為:

//com.ider.Dollar$Sign
佔位轉義Regex

$在Regex中,代表字串的結尾位置。比如下邊的運算式就是用來檢查字串是否以數字結尾的句子。

    var reg = /\d$/;    reg.test(str);

在一些Regex的實現中,$還可以作為運算式分隔字元(delimiter)以表示所包裹的內容是運算式的一部分,比如PHP就是如此:

    //match string that contain 'php'    $pattern = '$php$';    preg_match($pattern, $subject, $matches);

只是運算式的內容部分若遇到要用$,就不得不用斜杠(\)來轉義了,如果又要讓其真實去匹配$符號而非結尾,又要進一步轉義,然後就暈了。所以一般並不會使用$來做分隔字元。一般還是會用反斜線(/)來拼配JavaScript的格式,或者用#因為它被使用到較少。

Regex除了可以匹配,更重要的是用來替換,而替換中最強大的就是反向參考(Back-Reference)。多數支援Regex的文字編輯器都會使用反斜線加數字(\1, \2, \3 …)來表示反向參考的匹配內容。不過JavaScript則用$加數字來($1, $2, $3 …)表示。

    var friends = 'friends of Ider, friend of Angie';    var result = friends.replace(/(friends?) of (\w+)/g, "$2's $1");

這個在《JavaScript字串操作的偏方怪法》中有過介紹。

CSS

CSS的屬性選取器(Attribute Selector),不僅可以針對包含了指定屬性的標籤進行樣式設定,還能針對屬性所包含的內容。其中一個就是指定屬性內容是以什麼為結尾的,這與Regex很相似,同樣也很好用。比如希望針對jpg格式的圖片添加邊框讓她看起來像畫框,就可以

img[src$= ".jpg"] {      border: 10px solid #DDDDDD; }

 

取變數內容PHP

在PHP的變數中,總是會帶有$符號(define的常量除外),它可以看做是PHP變數的一部分,但是不嚴格地說它更像是取變數所包含的內容。比如下邊的代碼:

    $x=7;    $y="x";    echo $$y;

其結果是

7

第一個$去了y中的內容為x,第二個$取了x中的內容為7。另外->也可以理解以對象為目標取內容。

不只是PHP,Shell Script、Perl也是一樣的處理,像Shell Script就比較全別,不帶$的語句進行變數賦值,使用$來擷取變數的內容。

其它Bash Shell

Bash Shell裡面,$常常會被用來當做指令提示字元(Command Prompt)。我們也可以修改環境變數PS1來改變提示字元。

此外,$除了之前提到的當做取變數的內容外,同時也是一個變數,它儲存的是當前進程id。

iBatis

這是一個Java的ORM架構,不過我第一次接觸它是移植在.net平台上的iBatis.net。在這個架構裡,用$包裹變數則表示要對其內容進行轉義以防止SQL注入攻擊。或者使用#不進行轉義。

Visual FoxPro

cSearchFor $ cSearchIn來檢查字元式是否出現在另一個字元式中出現。

其它的其它

還有其它代碼中你看到$的不同用法,歡迎補充。不知道¥符號是否也能在代碼中有不同的功能呢。

References:
  1. Valid JavaScript variable names – Mathias Bynens
  2. Variable – The Java Tutorials
  3. The 30 CSS Selectors you Must Memorize – Nettuts+
  4. What are the special dollar sign shell variables? – stackoverflow
  歡迎到我的新部落格地址閱讀本文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.