# 调用位置和绑定规则

#### 1. 调用位置：寻找调用位置最重要的是分析调用栈（为了到达当前执行位置所调用的所有函数）。我们关心的调用位置就在当前正在执行的函数的前一个调用中。

```
function first() {
    //当前调用栈是： first
    //因此，当前调用位置是全局作用域

    console.log("first") ;
    second(); //<-- second 的调用位置
}


function second() {
    //当前调用位置是 first -> second
    // 因此，当前调用位置在 first 中

    console.log('second');
    third(); // third调用位置
}

function third() {
    //当前调用位置在 first -> second -> third
    // 当前调用位置在 second 中

    console.log("third");
}

first(); // first调用位置
```

#### 2. 绑定规则：

* **默认规则：**

独立函数调用时，可以把这条规则看作是无法应用其他规则时的默认规则。

```
function foo() {
    console.log(this.a)
}

var a  = 1;

foo(); // 1
```

解释：

* `foo()`是直接使用不带任何修饰的函数引用进行调用的，因此只能使用默认绑定，无法应用其他规则。
* 如果使用严格模式，则不能将全局变量用于默认绑定，因此this会绑定到`undefined`
  * 虽然this的绑定规则完全取决于调用位置，但是只有foo()运行在非`struct mode`时，默认绑定才能绑定到全局对象；在严格模式下调用`foo()`则不影响默认绑定；

    ```
    function foo(){
    console.log(this.a);
    }
    var a = 2;
    (function(){
    "use strict";

    foo(); // 2
    });
    ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heihei12305.gitbook.io/my-underscore/untitled/this/tiao-yong-wei-zhi-he-bang-ding-gui-ze.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
