場(chǎng)景:
現(xiàn)有一個(gè)對(duì)象, 通過(guò)prototype
關(guān)鍵字添加了方法foo()
:
var Graph = new Graph();
//foo
Graph.prototype.foo = function(){
...
}
//fun
Graph.prototype.fun = function(){
...
}
現(xiàn)在foo()
方法中的邏輯太復(fù)雜, 所以想要將某一功能代碼抽取成一個(gè)新的方法subfoo()
.
這個(gè)時(shí)候, 我應(yīng)該怎么做呢?
我開(kāi)始是這么做的:
var Graph = new Graph();
Graph.prototype.foo = function(){
....
function subfoo(){
//調(diào)用了fun()方法
this.fun(); //能夠成功調(diào)用graph的fun(), 但是走到fun()體中時(shí), this就變成了window, 預(yù)期的是想要依然指向graph
}
//調(diào)用subfoo()方法
subfoo.call(this); //讓this指向graph實(shí)例, 傳入subfoo()
}
但是, 當(dāng)我在subfoo()
中又調(diào)用了一個(gè)Graph.prototype.fun
方法時(shí), 走到fun()
時(shí), 其中的this
指針就會(huì)變成了window
, 即使使用call
也無(wú)效. 想要的當(dāng)然是一直指向graph.
可能是我的思路不對(duì), 像這種需要抽取方法的場(chǎng)景應(yīng)該怎么做呢?
完全沒(méi)有java
真正面向?qū)ο蟮膩?lái)的方便和易于理解呀!!!
func()
?哪來(lái)的func()
?能不能把你的代碼補(bǔ)充完整,包括你在哪里用了this
,并且把你期望這個(gè)this
指向哪個(gè)對(duì)象也補(bǔ)上。我猜你可能用了箭頭函數(shù),箭頭函數(shù)沒(méi)有this
關(guān)鍵字哦。
我試了一下以下代碼(手機(jī)上只有node,node的原型是__proto__
。瀏覽器上跑不方便)
function Graph() {}
var Graph = new Graph();
Graph.__proto__.foo = function() {
console.log('old foo', this);
return this;
}
Graph.__proto__.fun = function() {
console.log('old fun', this);
return this;
}
Graph.__proto__.foo = function() {
console.log('new foo', this);
function subfoo() {
console.log('subfoo', this);
this.fun();
return this;
}
subfoo.call(this);
return this;
}
Graph.foo();
結(jié)果是
new foo Graph {}
subfoo Graph {}
old fun Graph {}
Graph {}
非常的正常,不知道你有沒(méi)有漏了什么信息沒(méi)有提供的,當(dāng)然也有可能是瀏覽器和node的差別。另外你在什么瀏覽器上測(cè)試的?我中午或下午可以在瀏覽器上測(cè)試一下。
關(guān)鍵原因是因?yàn)槲以?code>Graph.prototype.fun()中使用了數(shù)組.foreach()
的代碼
這樣造成: Graph.prototype.foo
中調(diào)用 subfoo.call(this)
(沒(méi)有問(wèn)題,this指向符合預(yù)期), 但是subfoo()
中調(diào)用Graph.prototype.fun()
后, 在fun()
因?yàn)閳?zhí)行了數(shù)組.foreach()
, 這樣調(diào)用者不再是graph實(shí)例了, 那么在循環(huán)體里面, this自然就變成了全局window
.
所以關(guān)鍵原因是調(diào)用者發(fā)生了變化.
若將循環(huán)方式改成計(jì)數(shù)循環(huán), 則又OK.
修改后的代碼, this
指向正確:
for (var i = 0; i < edges.length; i++) {
var edge = edges[i];
if (edge.data != null && edge.data.type != null) {
console.log("this.edgeTypeSet");
console.log(this.edgeTypeSet);
if (edge.data.type in this.edgeTypeSet) {
console.log("this.edgeTypeSet:");
console.log(this.edgeTypeSet);
this.addEdge(edge);
}
} else { //若邊沒(méi)有type屬性, 則不受類(lèi)型開(kāi)關(guān)限制, 始終顯示
this.addEdge(edge);
}
}
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專(zhuān)業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。