JavaScript中prototype为对象添加属性的误区介绍
发布时间:2024-04-19 09:46:30
标签:JavaScript,prototype,对象,添加属性
先上需要用到的全部代码片段(截取)
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();
}
});
}
做的是谷歌的地图应用,其中有右方有两个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添加的属性和自身创建的局部变量关联起来,使其可被外部其它对象所调用获取
达到成功将同名属性通过类对象进行区分并全局调用


猜你喜欢
- 自动签到的python脚本源码新建一个python文件,checkin.py,保存到电脑上某个位置,我这里保存到的是E:\pyproject
- 这几天在落伍上转转,发现有朋友不太明白一些网站在会员注册时,当输入用户名后没按“确定”提交数据,系统也能马上检测该用户名是否已经存在。在此我
- SOLyog的下载、安装以及使用很简单。我去了相关网站下载,它只有384K字节大小。它把两个文件(一个可执行文件.exe和一个动态链接库文件
- 邮箱地址验证有很多方法。在浏览器端,js邮箱验证可以通过正则表达式检测。比如:function isEmail(email) {
- 首先数据库里需要有一个自动编号字段(ID)。然后第一次访问的时候,取出所有记录,定制好每页的记录数PageSize,计算出页数,然后根据页数
- 比较好的地方就是js数组的操作,不重复的数组id显示,完美实现。<script language="JavaScript&q
- 构筑专业的网络站点和应用程序,先进的设计工具,功能强大,开放式集成系统;流畅的开发进程。Macromedia Dreamweaver MX
- python3.6使用pymysql连接Mysql数据库及简单的增删改查操作,供大家参考,具体内容如下折腾好半天的数据库连接,由于之前未安装
- 代码如下#encoding:utf-8import requestsfrom lxml import etreeimport xlwtimp
- Python 中的 main 函数充当程序的执行点,在 Python 编程中定义 main 函数是启
- 在许多网页中,当鼠标移到一张图片上时,又弹出另一张图片,做这种广告条,要用到Macromedia DreamWeaver中的Lay
- Himmelblau函数如下:有四个全局最小解,且值都为0,这个函数常用来检验优化算法的表现如何:可视化函数图像:import numpy
- 前言??在vue项目中我们常常需要用到computed和watch,那么我们究竟在什么场景下使用computed和watch呢?他们之间又有
- 对于那些需要在登录环境下进行的爬虫操作,模拟登陆或伪装已登录状态是一个刚需。分析了网上关于模拟登录的例子,很多都基于用户名/密码发起一个po
- 数据库相关错误的解决办法错误一:数据库连接池超过限制SqlAlchemy QueuePool limit overflow造成连接数超过数据
- 引言在做项目的时候难免会遇到很多奇葩解析字符串的需求,简单的字符串通过内置方法就能解析出来,如果遇到复杂的就不好办了,那我们如何解决复杂字符
- 需求目标执行Python程序的时候在控制台输出内容的时候只显示一行,然后自动刷新内容,像这样:Downloading File FooFil
- 快速排序的基本思想:首先选定一个数组中的一个初始值,将数组中比该值小的放在左边,比该值大的放在右边,然后分别对左边的数组进行如上的操作,对右
- label与one-hot之间的互相转换有时候需要label,比如强化学习的离散动作空间,输出动作索引;有时候需要one-hot,比如训练数
- 之前的表单验证都是用js写的,这里也可以使用tp框架的验证。但是两者比较而言还是js验证比较好,因为tp框架验证会运行后台代码,这样运行速度