4

我正在制作一个 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")

所以我在我的内容脚本中尝试做的是将一个函数注入到原型中Match ,以获取变量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_red并且player_blue没有在this.

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

  • 即使通过 create new myMatch, newmyMatch也不会从其原始实例继承player_redand变量。player_blueMatch
  • 有没有可能的解决方法?谢谢。
4

1 回答 1

2

关于“部分解决方案”的说明:

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

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


鉴于上述情况,不可能提出确切的要求。

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

return但是,考虑这种有趣的方法,它利用了可以从构造函数中编辑显式对象的事实:

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.

快乐编码。


更新:

这是对上述内容的修改,以使用“将构造函数包装在新的构造函数中,然后将原型设置为相等”方法,如帖子中的链接中所述。诀窍是“窃取”全局属性名称。我还更改了代码以保持oldMatch“私密”以避免污染。

// 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.


如何从 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
于 2011-05-12T09:13:25.607 回答