我有一个值,它可能是一个基元或一个函数或一个包含基元/函数/对象递归的对象。javascript如何将一个对象深度绑定到一个新的“this”值
鉴于theThis
的说法,我如何将可能在我的价值范围内的所有功能深度绑定到theThis
?
我想是这样的:。
function deepBind(o, ths) {
Object.getOwnPropertyNames(o).forEach(key => {
const desc=Object.getOwnPropertyDescriptor(o, key);
if (typeof desc === "function") Object.defineProperty(o, key, key.bind(ths));
if (Object.getOwnPropertyNames(key).length>0) deepBind(o.key, ths);
});
}
但失败:(
我看着像https://github.com/jonschlinkert/deep-bind/blob/master/index.js一些解决方案,但就是不是独立的
我要寻找一个deepBind(val, theThis)
解决方案是独立的 我需要的解决方案还包括getter和setter。
Thx!
'我看了一些解决方案,并做了一些失败的尝试,但没有运气......比如? –
_“或递归地包含原始类型/函数/对象的对象”_你是什么意思“递归”? – guest271314
添加我的尝试 – kofifus