gpt4 book ai didi

javascript - 我如何摆脱这个函数中的 "with()"?

转载 作者:行者123 更新时间:2023-12-03 21:01:55 25 4
gpt4 key购买 nike

由于 with() 函数已被弃用,我想在我的代码中删除它。

在这个特定的函数中我该如何做?

原代码:

(function(a,b){
for(a in b=a.prototype)with({d:b[a]})b[a]=function(c){d.apply(this,arguments);return this}
})(Element);

引用格式化代码:

(function(a, b) {
for (a in b = a.prototype)
with({ d: b[a] })
b[a] = function(c) {
d.apply(this, arguments);
return this
}
})(Element);​

最佳答案

使用 with 的原因是关闭函数内 b[a] 的值,正确的替换是使用闭包:

(function(a, b) {
for (a in b = a.prototype)
(function (d) { //this line used to be: with({ d:b[a] })
b[a] = function(c) {
d.apply(this, arguments);
return this
}
}(b[a])); //this is where `d` is set
})(Element);​

关于javascript - 我如何摆脱这个函数中的 "with()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9640462/

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