1. 程式人生 > >函數中內置對象

函數中內置對象

con 工作 調用 被人 實參 code fine src ole

函數的3大內置對象,在這裏只寫了2個,其中一個比較重要,而且在工作中很多人搞不懂,this到底代表誰。所以我單獨寫出來了 函數中內置的三大對象 1、arguments對象 2、環境變量對象 3、this對象 arguments對象
用於保存實參(它只存在於函數中,函數外不能調用,具有數組所有的功能,但它不是數組) arguments對象通過索引值來訪問保存在其中的實參值
<script type="text/javascript">
function show(a,b){
    console.log(
        arguments[0], 必須有逗號
        arguments[
1] ) } show(3,5) </script> //結果是3 5
<script type="text/javascript">
function show(a,b){
    return arguments[0]+arguments[1];
}
console.log(show(3,5))
</script> //結果是8

aruments可以在函數裏面改變參數的賦值

<script type="text/javascript">
function show(){
    arguments[0]=5;
    return arguments[0
]+arguments[1] } console.log(show(3,4)) </script>//結果是9

練習題1

<script type="text/javascript">
function show(){
    var a=(arguments[1],arguments[0]+arguments[2]);
    return a;
}
    console.log(show(3,4,5))
</script>//結果是8  
註意:逗號運算符也是一個種賦值方式
一般是逗號和括號一起配套使用,可以連續賦值,單最後幾個是最後一個 練習題2
<script type="text/javascript">
var a=2; b=3; c=4;
function show(a,b,c){
    var a=(arguments[0]+arguments[2],b=10)
    return a,b
}
console.log(show(3,4,5))
</script> a b結果都是10

函數中 形參,實參與arguments之間的關系

註意:形參只從arguments裏拿值,只要實參不賦值,形參永遠是undefined 形參和實參並不是直接按照位置讀取的,其實中間還有一個arguments的中轉。首先實參保存到了 arguments裏,之形參在從arguments裏面按照順序讀取數值。 技術分享

環境變量對象(每個函數都有自己的環境變量對象)

函數內置對象,環境變量對象作用:保存函數內聲明的變量和函數。

但不能被人為訪問,只有用到時,系統自動去查詢。
<script type="text/javascript">
var color="red"
function show(){
    var color2="blue";
    function show2(){
        var color2=color;
        console.log(color2);
        function show3(){
            var color4=color2;
            console.log(color4);
        }
        show3()
    }
    show2()
    console.log(color2);
}
show()
</script>

技術分享

作用域鏈:作用域也叫執行環境,比如每個函數都有自己的一個執行函數。

函數中內置對象