我有一個值,它可能是一個基元或一個函數或一個包含基元/函數/對象遞歸的對象。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