JavaScript中prototype為對(duì)象添加屬性的誤區(qū)介紹
更新時(shí)間:2013年10月15日 16:32:02 投稿:whsnow
prototype為對(duì)象添加屬性的的過程中有些誤區(qū),在本文將為大家詳細(xì)介紹下,感興趣的朋友可不要錯(cuò)過
先上需要用到的全部代碼片段(截?。?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';
};
//圖層選擇開關(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)開關(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按鈕,通過點(diǎn)擊打開左方的div子菜單

要求是

打開前判斷該子菜單是否已經(jīng)為打開狀態(tài),如是,則先關(guān)閉,后打開
在開關(guān)子菜單時(shí),按鈕會(huì)據(jù)相應(yīng)行為變色
這里就要求在各個(gè)按鈕的show()方法下操作另一按鈕的屬性和方法來達(dá)到開關(guān)的效果
開始時(shí)寫成這樣
復(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é)果無論開關(guān)哪一個(gè)菜單,都只有“點(diǎn)”按鈕變色
原因大概是controlUI莫名定義為全局變量了
后來我試圖這樣
復(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é)果還是失敗
后來我想通了,大概這樣就可以了
復(fù)制代碼 代碼如下:
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';
}
這樣至少有錯(cuò)誤信息了,不能給undefined添加style屬性什么的
于是我絕望了,準(zhǔn)備給所有屬性也添加上全局變量,這樣調(diào)用就方便許多
沒成想,被自己啟發(fā)了
于是就有了最開始那段代碼
復(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)起來,使其可被外部其它對(duì)象所調(diào)用獲取
達(dá)到成功將同名屬性通過類對(duì)象進(jìn)行區(qū)分并全局調(diào)用
相關(guān)文章
JavaScript字符串對(duì)象fromCharCode方法入門實(shí)例(用于把Unicode值轉(zhuǎn)換為字符串)
這篇文章主要介紹了JavaScript字符串對(duì)象fromCharCode 方法入門實(shí)例,fromCharCode用于把Unicode值轉(zhuǎn)換為字符串,需要的朋友可以參考下2014-10-10關(guān)于JavaScript的Array數(shù)組方法詳解
這篇文章主要介紹了關(guān)于JavaScript的Array數(shù)組方法詳解,數(shù)組是一個(gè)固定長度的存儲(chǔ)相同數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu),數(shù)組中的元素被存儲(chǔ)在一段連續(xù)的內(nèi)存空間中,它是最簡單的數(shù)據(jù)結(jié)構(gòu)之一,需要的朋友可以參考下2023-05-05Javascript操作dom對(duì)象之select全面解析
下面小編就為大家?guī)硪黄狫avascript操作dom對(duì)象之select全面解析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04ASP小貼士/ASP Tips javascript tips可以當(dāng)桌面
今天看到《ASP小貼士/ASP Tips》 我也去把JavaScript的tips 下下來了。 看看是A4的。 自己把他改成1024 * 768 剛好可以用來做桌面2009-12-12javascript學(xué)習(xí)筆記(十三) js閉包介紹(轉(zhuǎn))
閉包(closure)是Javascript語言的一個(gè)難點(diǎn),也是它的特色,很多高級(jí)應(yīng)用都要依靠閉包實(shí)現(xiàn)2012-06-06javascript中數(shù)組的多種定義方法和常用函數(shù)簡介
本文簡單介紹了javascript一維數(shù)組和二維數(shù)組的定義方法集錦以及常用函數(shù)簡介。2014-05-05Javascript模塊化編程(三)require.js的用法及功能介紹
這個(gè)系列的第一部分和第二部分,介紹了Javascript模塊原型和理論概念,今天介紹如何將它們用于實(shí)戰(zhàn)。我采用的是一個(gè)非常流行的庫require.js感興趣的朋友可以了解下啊2013-01-01