Javascript:對象/函數/運算子,javascript運算子

來源:互聯網
上載者:User

Javascript:對象/函數/運算子,javascript運算子

1.Javascript對象可以直接建立,而無需像java等其它語言那樣先定義一個類。
2.由於Javascript是弱類型,因此定義函數時也不需要寫參數的類型類型。不需要聲明返回值,在函數體中可自由確定是否return一個值。
3.Javascript運算子與java非常類似。

代碼整理自w3school:http://www.w3school.com.cn 

範例程式碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><title>Javascript 對象與函數</title><head type="text/css">  <style>    body {background-color:#e0e0e0}    p.nowrap {display:inline;padding-left:10px}    p.percent40w {width:40%}    span.redColor {color:red}    input.niceStyle {padding:5px}  </style></head><body>  <p>JavaScript 中的所有事物都是對象:字串、數字、數組、日期,等等。</p>  <h3>(一)建立一個Javascript對象</h3>  <script>    person = new Object();    person.firstName = "Bill";    person.lastName = "Gates";    person.age = 56;    person.speak = function(){      alert("I am a person!");    }    //訪問對象的屬性    document.write(person.firstName+" "+person.lastName+" is "+person.age+" years old.<br/>" );    document.write("eyescolor:"+person.eyescolor+"<br/>");    //訪問對象的方法    //person.speak();    var s = "Hello World"    document.write("uppercase:"+s.toUpperCase()+"<br/>");    document.write("length:"+s.length+"<br/>");      </script>  <h3>(二)帶參數的函數</h3>  <p>點擊按鈕,調用帶參數的函數</p>  <button onclick="myFunction('Bill Gates','CEO')">點擊這裡</button>  <script>    function myFunction(name,job){      alert("Welcome " + name + ", the " + job);    }  </script>  <h3>(三)帶傳回值的函數</h3>  <input class = "niceStyle" id = "originalText" type="text"/>  <button type="button" onclick="toUpper()">轉為大寫</button><br/>  <p class = "nowrap" id = "upperText"></p>  <script>    function toUpper(){      var x = document.getElementById("originalText").value;      document.getElementById("upperText").innerHTML = getUpper(x);    }    function getUpper(originalText){      return originalText.toUpperCase()    }  </script>  <h3>(四)JavaScript變數的範圍</h3><p class="percent40w">1.在函數內部聲明的變數(使用 var)是局部變數,只能在函數內部訪問。可以在不同的函數中使用名稱相同的局部變數,因為只有聲明過該變數的函數才能識別出該變數。<br/>2.在函數外聲明的變數是全域變數,<span class = "redColor">網頁上的所有指令碼和函數</span>都能訪問它。<br/>3.局部變數會在函數運行以後被刪除,全域變數會在頁面關閉後被刪除。<br/>4.如果把值賦給尚未聲明的變數,該變數將被自動作為全域變數聲明。</p>  <h3>(五)JavaScript運算子</h3>  <p>如果把數字與字串相加,結果將成為字串。此規則與java完全相同。</p>  <script>    var x = 5+6;    document.write(x)    document.write("<br/>")    x = "5"+"6"    document.write(x)    document.write("<br/>")    x = 5 +"6"    document.write(x)    document.write("<br/>")    x = "5"+6    document.write(x)    document.write("<br/>")    x = 5+6+"6"    document.write(x)    document.write("<br/>")  </script>  </body></html>


聯繫我們

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