gpt4 book ai didi

javascript - 访问闭包范围内的私有(private)(本地)变量

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:21:45 25 4
gpt4 key购买 nike

我正在制作 google chrome 扩展并尝试在闭包范围内获取局部变量的引用。

// The script model of the target website
// I can't change any code of these
function Player(playerName){
this.name = playerName;
this.score = 0;
}

function Match(playerRed,playerBlue){
var player_red = new Player(playerRed);
var player_blue = new Player(playerBlue);
}

var tennis = new Match("Mike","John")

所以我在我的内容脚本中尝试做的是将一个函数注入(inject)到 Match 的原型(prototype)中只是为了获取变量 player_redplayer_blue:

function Match(playerRed,playerBlue){
var player_red = new Player(playerRed);
var player_blue = new Player(playerBlue);

//hoping to add this into Match.prototype
this.showMatchInfo = function(){
alert(player_red.name + " vs " + player_blue.name);
}
}

但这不会起作用,因为 player_redplayer_blue 未在 this 下定义。

我找到了这个 question通过搜索。解决方案是“将构造函数包装在一个新的构造函数中,然后将原型(prototype)设置为相等”。不幸的是,这对我不起作用,因为我无法访问网站的原始脚本,可能是因为:

  • 即使创建新的 myMatch,新的 myMatch 也不会继承 player_redplayer_blue 变量来自他们原来的 Match 实例。
  • 是否有任何可能的解决方法?谢谢。

最佳答案

关于“部分解决方案”的注释:

请注意,下面发布的代码片段仅显示“一些可能或可能无法提供足够的替代方案”。这是因为它们不捕获构造函数中的值(Player 对象),而只是包装内部的值。

“完整解决方案”还可以包装 Player 构造函数并使用属性或其他机制来“记住”为不同输入值创建的对象;或者,它可以记住对象创建顺序。然后,这可用于包装 Match,然后在 Match 构造函数运行后从共享存储中提取创建的 Players —— 但是,这些细节留作练习。 Player 包装代码可以使用下面显示的代码(假设 Player 是全局/可访问属性)。


鉴于上述上下文,不可能有确切的请求。

变量(真正的变量,不是属性)只能从它们声明的范围或嵌套范围访问,因为它们是通过范围链解析的。这也包括 eval 的使用。虽然这看起来像是一个限制,但它也确保了作用域链(及其变量)不会被外部破坏,除非暴露。

然而,考虑一下这种有趣的方法,它利用了显式对象可以从构造函数返回的事实:

var oldMatch = Match
// note this form, else above would be pre-clobbered
Match = function Match (playerRed, playerBlue) {
var m = new oldMatch(playerRed, playerBlue)
// either "inject" method here, or save in object for later
m.myPlayerRed = playerRed
m.myPlayerBlue = playerBlue
return m
}

当然,这会破坏 new Match(...) instanceof Match 之类的东西。

快乐编码。


更新:

这里是对上面的修改,以使用“将构造函数包装在一个新的构造函数中,然后将原型(prototype)设置为相等”方法,如帖子中的链接中所讨论的那样。诀窍是“窃取”全局属性名称。我还更改了代码以保持 oldMatch“私有(private)”以避免污染。

// note this form, else Match property would be pre-clobbered
Match = (function (oldMatch) {
function Match (playerRed, playerBlue) {
oldMatch.call(this, playerRed, playerBlue);
// either "inject" method here, or save in object for later
this.myPlayerRed = playerRed
this.myPlayerBlue = playerBlue
}
Match.prototype = oldMatch.prototype
return Match
})(Match)

与第一个代码片段不同,这应该与 new Match(...) instanceof Match 一起使用,但它仍然可能会中断,具体取决于在 Match 对象方法中所做的特定假设。


如何从 Player 构造函数中反转(“提取”)数据的示例:

// original -- remember this method will only work
// if Player is used as a property (and not itself a closure'd variable)
function Player (name) {
this.name = name
}

Player = (function (oldPlayer) {
function Player (name) {
oldPlayer.call(this, name)
var fn = arguments.callee
fn.recent = fn.recent || []
fn.recent.push([name, this])
}
Player.prototype = oldPlayer.prototype
return Player
})(Player)

var p1 = new Player("fred");
var p2 = new Player("barney");

alert("instanceof check? " + p1 instanceof Player)
alert("name check? " + ("barney" == p2.name))

alert(Player.recent.join(","))
Player.recent = [] // reset

关于javascript - 访问闭包范围内的私有(private)(本地)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975631/

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