JavaScript中prototype為對象添加屬性的誤區介紹_基礎知識

來源:互聯網
上載者:User
先上需要用到的全部程式碼片段(截取)
複製代碼 代碼如下:

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';
};
//圖層選擇開關
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 = '點擊啟用菜單';
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();
}
});
}
//點開關框體
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 = '點擊操控點菜單';
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(pointControl.boxDisplay){
pointControl.hide();
}else{
pointControl.show();
}
});
}

做的是Google的地圖應用,其中有右方有兩個div按鈕,通過點擊開啟左方的div子功能表
 
要求是
 
開啟前判斷該子功能表是否已經為開啟狀態,如是,則先關閉,後開啟

在開關子功能表時,按鈕會據相應行為變色

這裡就要求在各個按鈕的show()方法下操作另一按鈕的屬性和方法來達到開關的效果

開始時寫成這樣
複製代碼 代碼如下:

MenuControl.prototype.controlUI;
MenuControl.prototype.show = function(){
controlUI.style.backgroundColor = '#DDDDDD';//直接調用屬性
};
function MenuControl(controlDiv, map) {
controlUI = document.createElement('div');
controlUI.style.backgroundColor = 'white';
}

結果無論開關哪一個菜單,都只有“點”按鈕變色

原因大概是controlUI莫名定義為全域變數了

後來我試圖這樣
複製代碼 代碼如下:

MenuControl.prototype.controlUI;
MenuControl.prototype.show = function(){
this.controlUI.style.backgroundColor = '#DDDDDD';//添加this關鍵字
};
function MenuControl(controlDiv, map) {
controlUI = document.createElement('div');
controlUI.style.backgroundColor = 'white';
}

結果還是失敗

後來我想通了,大概這樣就可以了
複製代碼 代碼如下:

MenuControl.prototype.controlUI.style.backgroundColor = "white";//一上來就給你賦值,看你往哪兒跑
MenuControl.prototype.show = function(){
this.controlUI.style.backgroundColor = '#DDDDDD';
};
function MenuControl(controlDiv, map) {
controlUI = document.createElement('div');
this.controlUI.style.backgroundColor = 'white';
}

這樣至少有錯誤資訊了,不能給undefined添加style屬性什麼的

於是我絕望了,準備給所有屬性也添加上全域變數,這樣調用就方便許多

沒成想,被自己啟發了

於是就有了最開始那段代碼
複製代碼 代碼如下:

MenuControl.prototype.controlUI;//先建立此屬性,挖一個坑
MenuControl.prototype.show = function(){
this.controlUI.style.backgroundColor = '#DDDDDD';//使用this關鍵字調用,實際調用的是this.controlUI對象
};
function MenuControl(controlDiv, map) {
var controlUI = document.createElement('div');//建立局部變數,並正常賦值
this.controlUI = controlUI;//將此局部變數反賦給this對象的屬性,達到關聯引用
controlUI.style.backgroundColor = 'white';//正常調用引用對象進行操控
}

這樣就將prototype添加的屬性和自身建立的局部變數關聯起來,使其可被外部其它對象所調用擷取

達到成功將同名屬性通過類對象進行區分並全域調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.