[英]Access private (local) variable inside a closure scope
我正在做一個谷歌瀏覽器擴展程序,並試圖獲取閉包范圍內的局部變量的引用。
// 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_red
和player_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);
}
}
但這不起作用,因為未在this
下定義player_red
和player_blue
。
我通過搜索發現了這個問題 。 解決方案是“將構造函數包裝在新的構造函數中,然后將原型設置為相等” 。 不幸的是,這對我不起作用,因為我無法訪問網站的原始腳本,並且可能是因為:
myMatch
,新的myMatch
也不會從其原始Match
實例中繼承player_red
和player_blue
變量。 關於“部分解決方案”的注釋:
請注意,下面發布的代碼段僅顯示“可能無法提供足夠的替代方法”。 這是因為它們沒有捕獲構造函數中的值(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
,但仍可能會中斷,具體取決於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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.