JavaScript中prototype為對(duì)象添加屬性的誤區(qū)介紹
更新時(shí)間:2013年10月15日 16:32:02 投稿:whsnow
prototype為對(duì)象添加屬性的的過(guò)程中有些誤區(qū),在本文將為大家詳細(xì)介紹下,感興趣的朋友可不要錯(cuò)過(guò)
先上需要用到的全部代碼片段(截?。?br />
復(fù)制代碼 代碼如下:
MenuControl.prototype.boxDisplay = false;//是否顯示圖層選擇菜單
MenuControl.prototype.controlUI;
MenuControl.prototype.show = function(){
if(pointControl.boxDisplay){
pointControl.hide();
}
menuBoxDiv.style.display = "";
this.boxDisplay = true;
this.controlUI.style.backgroundColor = '#DDDDDD';
};
MenuControl.prototype.hide = function(){
menuBoxDiv.style.display = "none";
this.boxDisplay = false;
this.controlUI.style.backgroundColor = 'white';
};
//圖層選擇開(kāi)關(guān)
function MenuControl(controlDiv, map) {
controlDiv.style.padding = '5px';
var controlUI = document.createElement('div');
this.controlUI = controlUI;
controlUI.style.backgroundColor = 'white';
controlUI.style.height = '18px';
controlUI.style.borderStyle = 'solid';
controlUI.style.borderWidth = '1px';
controlUI.style.cursor = 'pointer';
controlUI.style.textAlign = 'center';
controlUI.title = '點(diǎn)擊啟用菜單';
controlDiv.appendChild(controlUI);
var controlText = document.createElement('div');
controlText.style.fontFamily = 'Arial,sans-serif';
controlText.style.fontSize = '12px';
controlText.style.paddingLeft = '4px';
controlText.style.paddingRight = '4px';
controlText.innerHTML = '<strong>圖層選擇</strong>';
controlUI.appendChild(controlText);
google.maps.event.addDomListener(controlUI, 'click', function() {
if(menuControl.boxDisplay){
menuControl.hide();
}else{
menuControl.show();
}
});
}
//點(diǎn)開(kāi)關(guān)框體
PointControl.prototype.boxDisplay = false;//是否顯示圖層選擇菜單
PointControl.prototype.controlUI;
PointControl.prototype.show = function(){
if(menuControl.boxDisplay){
menuControl.hide();
}
pointBoxDiv.style.display = "";
this.boxDisplay = true;
this.controlUI.style.backgroundColor = '#DDDDDD';
};
PointControl.prototype.hide = function(){
pointBoxDiv.style.display = "none";
this.boxDisplay = false;
this.controlUI.style.backgroundColor = 'white';
};
function PointControl(controlDiv, map) {
controlDiv.style.padding = '5px';
var controlUI = document.createElement('div');
this.controlUI = controlUI;
controlUI.style.backgroundColor = 'white';
controlUI.style.height = '18px';
controlUI.style.borderStyle = 'solid';
controlUI.style.borderWidth = '1px';
controlUI.style.cursor = 'pointer';
controlUI.style.textAlign = 'center';
controlUI.title = '點(diǎn)擊操控點(diǎn)菜單';
controlDiv.appendChild(controlUI);
var controlText = document.createElement('div');
controlText.style.fontFamily = 'Arial,sans-serif';
controlText.style.fontSize = '12px';
controlText.style.paddingLeft = '4px';
controlText.style.paddingRight = '4px';
controlText.innerHTML = '<strong>點(diǎn)</strong>';
controlUI.appendChild(controlText);
google.maps.event.addDomListener(controlUI, 'click', function() {
if(pointControl.boxDisplay){
pointControl.hide();
}else{
pointControl.show();
}
});
}
做的是谷歌的地圖應(yīng)用,其中有右方有兩個(gè)div按鈕,通過(guò)點(diǎn)擊打開(kāi)左方的div子菜單
要求是
打開(kāi)前判斷該子菜單是否已經(jīng)為打開(kāi)狀態(tài),如是,則先關(guān)閉,后打開(kāi)
在開(kāi)關(guān)子菜單時(shí),按鈕會(huì)據(jù)相應(yīng)行為變色
這里就要求在各個(gè)按鈕的show()方法下操作另一按鈕的屬性和方法來(lái)達(dá)到開(kāi)關(guān)的效果
開(kāi)始時(shí)寫(xiě)成這樣
復(fù)制代碼 代碼如下:
MenuControl.prototype.controlUI;
MenuControl.prototype.show = function(){
controlUI.style.backgroundColor = '#DDDDDD';//直接調(diào)用屬性
};
function MenuControl(controlDiv, map) {
controlUI = document.createElement('div');
controlUI.style.backgroundColor = 'white';
}
結(jié)果無(wú)論開(kāi)關(guān)哪一個(gè)菜單,都只有“點(diǎn)”按鈕變色
原因大概是controlUI莫名定義為全局變量了
后來(lái)我試圖這樣
復(fù)制代碼 代碼如下:
MenuControl.prototype.controlUI;
MenuControl.prototype.show = function(){
this.controlUI.style.backgroundColor = '#DDDDDD';//添加this關(guān)鍵字
};
function MenuControl(controlDiv, map) {
controlUI = document.createElement('div');
controlUI.style.backgroundColor = 'white';
}
結(jié)果還是失敗
后來(lái)我想通了,大概這樣就可以了
復(fù)制代碼 代碼如下:
MenuControl.prototype.controlUI.style.backgroundColor = "white";//一上來(lái)就給你賦值,看你往哪兒跑
MenuControl.prototype.show = function(){
this.controlUI.style.backgroundColor = '#DDDDDD';
};
function MenuControl(controlDiv, map) {
controlUI = document.createElement('div');
this.controlUI.style.backgroundColor = 'white';
}
這樣至少有錯(cuò)誤信息了,不能給undefined添加style屬性什么的
于是我絕望了,準(zhǔn)備給所有屬性也添加上全局變量,這樣調(diào)用就方便許多
沒(méi)成想,被自己?jiǎn)l(fā)了
于是就有了最開(kāi)始那段代碼
復(fù)制代碼 代碼如下:
MenuControl.prototype.controlUI;//先建立此屬性,挖一個(gè)坑
MenuControl.prototype.show = function(){
this.controlUI.style.backgroundColor = '#DDDDDD';//使用this關(guān)鍵字調(diào)用,實(shí)際調(diào)用的是this.controlUI對(duì)象
};
function MenuControl(controlDiv, map) {
var controlUI = document.createElement('div');//建立局部變量,并正常賦值
this.controlUI = controlUI;//將此局部變量反賦給this對(duì)象的屬性,達(dá)到關(guān)聯(lián)引用
controlUI.style.backgroundColor = 'white';//正常調(diào)用引用對(duì)象進(jìn)行操控
}
這樣就將prototype添加的屬性和自身創(chuàng)建的局部變量關(guān)聯(lián)起來(lái),使其可被外部其它對(duì)象所調(diào)用獲取
達(dá)到成功將同名屬性通過(guò)類(lèi)對(duì)象進(jìn)行區(qū)分并全局調(diào)用
相關(guān)文章
淺析四種常見(jiàn)的Javascript聲明循環(huán)變量的書(shū)寫(xiě)方式
這篇文章主要介紹了四種常見(jiàn)的聲明循環(huán)變量的書(shū)寫(xiě)方式,對(duì)其進(jìn)行簡(jiǎn)單的分析和比較,需要的朋友可以參考下2015-10-10
JavaScript入門(mén)教程(9) Document文檔對(duì)象
Document文檔對(duì)象是JavaScript中window和frames對(duì)象的一個(gè)屬性,是顯示于窗口或框架內(nèi)的一個(gè)文檔。2009-01-01
每日十條JavaScript經(jīng)驗(yàn)技巧(二)
本文是每日十條JavaScript經(jīng)驗(yàn)技巧系列文章的第二篇,同樣給大家匯總介紹10條個(gè)人在項(xiàng)目中的一些經(jīng)驗(yàn),分享給大家,希望大家能夠喜歡2016-06-06
js setTimeout 常見(jiàn)問(wèn)題小結(jié)
主要包括this指向問(wèn)題、向setTimeout傳入?yún)?shù)等相關(guān)問(wèn)題,下面與大家分享下以上問(wèn)題的解決方法,感興趣的朋友可以參考下2013-08-08

