【发布时间】:2020-09-22 15:44:35
【问题描述】:
对于原型上的函数,我曾经做过类似的事情:
var attachShadow = HTMLElement.prototype.attachShadow
HTMLElement.prototype.attachShadow = function (option) {
var sh = attachShadow.call(this, option)
console.log('ShadowDOM Attached!')
return sh
}
在此示例中,我修改了 HTMLElement 原型中的 attachShadow 方法,以便在新的 shadowDOM 附加到元素时通知我。
现在我想对ShadowRoot.prototype.adoptedStyleSheets 做类似的事情,但是这次adoptedStyleSheets 是一个getter/setter,var adoptedStyleSheets = HTMLElement.prototype.adoptedStyleSheets 会导致错误:Uncaught TypeError: Illegal invocation。
我不知道该怎么做,如何修改原型上的 getter/setter?
【问题讨论】:
-
@luekbaja 相同的错误,但情况不同。
标签: javascript prototype getter-setter