> For the complete documentation index, see [llms.txt](https://blog.yuyy.tech/mian-shi-shu-li/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yuyy.tech/mian-shi-shu-li/jing-dian-mian-shi-ti/jian-tou-han-shu-this1.md).

# 箭头函数this-1

## 题目

说明运行结果及原因

```javascript
let Tom = {
  name: "Tom",
  sayHi: function () {
    console.log(`Hi, I am ${this.name}!`);
  },
  showTitle(...titles) {
    this.sayHi();
    console.log(`my title: ${[...titles].join("、")}`);
  },
};

let Jerry = {
  name: "Jerry",
  sayHi: () => console.log(`Hi, I am ${this.name}!`),
};

Tom.showTitle.call(Jerry, "developer", "engineer");

```

## **答案**

浏览器环境：

> Hi, I am !
>
> my title: developer、engineer

node环境（v16.17.1）：

> <mark style="color:red;">TypeError: Cannot read properties of undefined (reading 'name')</mark>
>
> <mark style="color:red;">sayHi: () => console.log(</mark><mark style="color:red;">`Hi, I am ${this.name}!`</mark><mark style="color:red;">)</mark>
>
> &#x20;                                                                             <mark style="color:red;">^</mark>

解释：

浏览器环境：Jerry的sayHi函数的作用域的上一层是全局，此时this.name应该是window上的name，但是window上没有声明name属性，所以为空；

Node环境：node环境的全局this是undefined，所以报错

## 知识点

* <mark style="color:red;">箭头函数的this指向其声明时作用域的上一层。</mark>
* <mark style="color:red;">函数的call作用</mark>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://blog.yuyy.tech/mian-shi-shu-li/jing-dian-mian-shi-ti/jian-tou-han-shu-this1.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.
