gpt4 book ai didi

javascript - 属于 JavaScript 对象原型(prototype)的回调函数可以访问对象成员吗?

转载 作者:行者123 更新时间:2023-12-02 07:46:56 27 4
gpt4 key购买 nike

属于 JavaScript 对象原型(prototype)的回调函数如何访问对象成员?回调不能关闭,一切都必须定义如下:

function Obji(param){
this.element = param;
}

Obji.prototype.func(){
database.get("someKey",this.cb);
}

Obji.prototype.cb(){
//here I would like to access this.element
}

最佳答案

database.get("someKey",this.cb.bind(this));

.bind , ES5 shim对于旧浏览器

关于javascript - 属于 JavaScript 对象原型(prototype)的回调函数可以访问对象成员吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com