JavaScript函數認識,Js中的常見函數

來源:互聯網
上載者:User

標籤:lis   開頭   定位   javascrip   形參   class   代碼   指定   replace   

JavaScript函數:

也稱為方法,用來儲存一塊代碼,需要的時候調用。

函數是由事件驅動的或者當它被調用時執行的可重複使用的代碼塊。

函數需要包含四要素:傳回型別,函數名,參數列表,函數體

拓展:強型別語言的函數

public int Sun(int a,int b){    return = a+b;}

return返回,Sun函數名,int a,int b,參數列表,int 整型。

沒有傳回值的函數:

public void Sun(int a,int b){}Sun(1,2);

這樣的參數a,b是形參,也就是形式參數,調用函數是給的參數1,2是實參,也就是實際參數。

JavaScript中的函數定義:

    //定義函數jiSun    function jiSuan(){    alert("這是函數jiSuan");    }    //調用函數    jiSuan();

function是定義函數,並不會執行,調用函數時才會尋找該函數名的定義內容。

JavaScript中函數的定義和調用先後順序可以先寫調用在寫定義。

有參數的函數:

    //有參數的函數    function jiSuan(a,b){        alert(a+b);    }    //調用函數    jiSuan(3,5);

需要注意的是定義函數是的形參並不需要用var定義。

有傳回值的函數:

    function jiSuan(a,b){        return a+b;    }    //調用函數    var c=jiSuan(3,5);    alert(c);

傳回值返回給調用函數,一般定義一個變數把傳回值賦給變數。

 

補充:強型別語言中有預設值的函數,js不支援有預設值的函數

    function jiSuan(a,b=2){        alert(a+b);    }    //調用函數    jiSuan(3);
JavaScript中的常用函數:

document.write(""); 輸出語句

Math.random();擷取0-1之間的隨機數

    document.write(Math.random());

    document.write(parseInt(Math.random()*10));

日期時間類函數:
    //擷取目前時間    document.write(Date());

    //擷取目前時間    var d=new Date();    //擷取目前時間戳    document.write(d.getTime());

    //擷取目前時間    var d=new Date();    //擷取當前年份    document.write(d.getFullYear());
    //擷取目前時間    var d=new Date();    //擷取當前月份,注意這裡需要+1    document.write(d.getMonth()+1);
    //擷取目前時間    var d=new Date();    //擷取當前幾號    document.write(d.getDate());
    //擷取目前時間    var d=new Date();    //擷取當前幾時    document.write(d.getHours());
    //擷取目前時間    var d=new Date();    //擷取當前幾分    document.write(d.getMinutes());
    //擷取目前時間    var d=new Date();    //擷取當前幾秒    document.write(d.getSeconds());
    //擷取目前時間    var d=new Date();    //擷取當前星期幾    document.write(d.getDay());
    //擷取目前時間    var d=new Date();    //擷取當前幾毫秒    document.write(d.getMilliseconds());
數學類函數:
    //向上取整    document.write(Math.ceil(3.5));

    //向下取整    document.write(Math.floor(3.5));

    //取絕對值    document.write(Math.abs(-2));    //四捨五入    document.write(Math.round(5.5));    //返回最高值    document.write(Math.max(5,7));    //返回最低值    document.write(Math.round(5.7));    //返回兩個數的次冪    document.write(Math.pow(5.7));    //返回平方根    document.write(sqrt.round(5));
字串函數:
    var str="hello world";    var s="l";    //返回字元在字串中第一次出現的位置    document.write(str.indexOf(s));    //返回指定位置的字元    document.write(str.charAt(0));    //返回字元在字串中最後一次出現的位置    document.write(str.lastIndexOf(s));    //截取字串    document.write(str.substring(1,3));    //截取字串相應的長度    document.write(str.substr(1,3));

    var str="hello world";    //替換相應字串    str=str.replace("hell","^^");    document.write(str);

    var str="hello world";    //替換所有相應字串    str=str.replace(/l/g,"^^");    document.write(str);

 

 

    //split拆分字串,通過將字串劃分成子串,將一個字串做成一個字串數組。    var str="hello world";    var arr=str.split(" ");

如上字串"helllo world"會被空格拆分成數組,第一個值hello,第二個值world

其他:

length 屬性 
返回字串的長度,所謂字串的長度是指其包含的字元的個數。

 

toLowerCase
將整個字串轉成小寫字母。
var lower_string = a.toLowerCase();
//lower_string = "hello"

 

toUpperCase
將整個字串轉成大寫字母。
var upper_string = a.toUpperCase();
//upper_string = "HELLO"

 

search
執行一個Regex匹配尋找。如果尋找成功,返回字串中匹配的索引值。否則返回 -1 。
var index1 = a.search(re);
//index1 = 0
var index2 = b.search(re);
//index2 = -1

補充:

變數名的命名規範:一般以字母開頭,一般都用小寫字母,盡量不出現特殊符號

函數名的命名規範:駝峰法,首字母小寫,其他每個單字首大寫

JavaScript函數認識,Js中的常見函數

聯繫我們

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