const userList = []; function emit (...newValue) { console.log(newValue); } const proxy = new Proxy(userList, { set(target, property, value, receiver) { const result = Reflect.set(...arguments); if(result) { emit(Reflect.get(target, property, receiver)); } return result; } }) proxy.push('你好');为什么输出结果是['你好'][1]
时间: 2023-02-11 09:17:41 浏览: 64
这段代码中,定义了一个空数组userList,并使用了 JavaScript 中的 Proxy 对象来代理这个数组。在代理对象中,定义了一个set拦截器,当给数组新增元素时,会触发这个拦截器。
在拦截器中,首先使用Reflect.set方法给数组新增元素,并将返回值赋值给result。然后使用emit函数输出新增元素的值,并返回result。
所以最后proxy.push("u4f60u597d")这一行会在数组中新增一个元素 “u4f60u597d” 同时输出”[‘u4f60u597d’][1]”,数组新增元素之后的长度是1.
相关问题
const userList = []; function emit (...newValue) { console.log(newValue); } const proxy = new Proxy(userList, { set(target, property, value, receiver) { const result = Reflect.set(...arguments); if(result) { emit(Reflect.get(target, property, receiver)); } return result; } }) proxy.push('你好');中return result;有什么用
在这个代码片段中,`proxy` 是一个由 `userList` 对象创建的代理。当通过代理对象来设置值时,会触发 `set` 方法。`set` 方法中的 `return result;` 语句的作用是返回 `Reflect.set()` 方法的返回值,它表示设置操作是否成功。如果设置成功,返回 `true`,如果失败,返回 `false`。
在uniapp中,console.log(this.userList) 不起作用 。 function getList() { user.getUserList().then(res => { console.log('getUserList>>>>>>>>>>>') console.log(res) const userList = res.result.records; console.log(userList) console.log(this.userList) this.userList = userList; }).catch(error => { }) }
在 `getList` 函数中,`console.log(this.userList)` 不起作用的原因是 `this` 指向的是当前函数的执行上下文,而不是组件实例。因此,代码中的 `this.userList` 并不是组件实例中定义的 `userList` 数据,而是一个未定义的变量。
为了解决这个问题,你可以在 `getList` 函数中将组件实例赋值给一个变量,然后使用该变量来访问组件数据。以下是一个修改后的示例:
```
function getList() {
const vm = this // 将组件实例赋值给变量 vm
user.getUserList().then(res => {
console.log('getUserList>>>>>>>>>>>')
console.log(res)
const userList = res.result.records;
console.log(userList)
console.log(vm.userList) // 使用 vm 访问组件数据
vm.userList = userList; // 更新组件数据
}).catch(error => {
// 异常处理
})
}
```
在上面的示例中,我们将组件实例赋值给了变量 `vm`,然后使用 `vm.userList` 来访问组件数据。这样就能够正确地更新组件数据了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)