原生js面向对象写法
Mouse就是一个类,有自己的成员变量和成员方法,成员方法一定加上prototype,避免js原型的坑。
var Mouse = function(id)
{ this.id = id;
this.name = "";
this.mes = null;//被创建的那个div
this.con = null;
this.runAwayInterval = null; this.init();
}; Mouse.prototype.init = function()
{
// console.log("初始化id为 " + this.id + " 的mouse");
this.show(); } Mouse.prototype.show = function()
{
this.mes = document.createElement("div");
this.mes.setAttribute("id","mickey");
this.con = document.getElementById("container");
this.mes.style.opacity = 1;
this.con.appendChild(this.mes); this.mes.onclick = function()
{
getScore();
this.con.removeChild(this.mes);
clearInterval(this.runAwayInterval);
removeOneMouse(this.id);
}.bind(this);
// console.log(this.con.offsetWidth - 100);
this.mes.style.left = Math.random()*(this.con.offsetWidth - 100).toString()+"px";
var targetTop = Math.random()*(this.con.offsetHeight - 100) +50;
this.mes.style.top =targetTop +"px";
// this.mes.style.top = 0 +"px"; this.runAwayInterval = setInterval(this.runAway.bind(this),200);
} Mouse.prototype.runAway = function()
{
// console.log("我是' "+ this.id +" '我正在跑..."); var opa = parseFloat(this.mes.style.opacity);
opa -= 0.1;
this.mes.style.opacity = opa;
if(opa<=0)
{
this.lose();
}
} // Mouse.prototype.beCatch = function()
// {
// this.con.removeChild(this.mes);
// clearInterval(this.runAwayInterval);
// } Mouse.prototype.lose = function()
{
// console.log("我是' "+ this.id +" '我成功跳走了...");
this.con.removeChild(this.mes);
clearInterval(this.runAwayInterval);
removeOneMouse(this.id);
loseScore();
}
原生js面向对象写法的更多相关文章
- 原生JS面向对象思想封装轮播图组件
原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...
- 原生js面向对象编程-选项卡(自动轮播)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 原生js面向对象编程-选项卡(点击)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- screen,client,page三种确定鼠标坐标的区别和原生JS事件写法,区别于Jquery的$.on(x,y);和$.click()
screenX clientX pageX的区别 screenX:鼠标位置相对于用户屏幕水平偏移量,而screenY也就是垂直方向的,此时的参照点也就是原点是屏幕的左上角. clientX:跟scre ...
- 常用原生JS兼容性写法汇总
1.添加事件方法 addHandler:function(element,type,handler){ if(element.addEventListener){//检测是否为DOM2级方法 elem ...
- 原生JS面向对象方法实现万年历
###面向对象的方法实现万年历 实现思路: 1.创建构造函数constructor ``` function Calender(main){ this.current ...
- 【JavaScript】两种常见JS面向对象写法
基于构造函数 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...
- js面向对象写法及栈的实现
function Stack() { this.dataStore = []; this.top = 0; //指向栈顶的位置 this.push = push; this.pop = pop; th ...
- 【CSS进阶】原生JS getComputedStyle等方法解析
最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...
随机推荐
- javaEE中的spring配置笔记
0 JavaEE的工程目录 0.1 WebContent 项目的主目录,在eclipse新建工程时可以自己命名,部署时会把该文件夹的内容发布到tomcat的webapps里. 该目录下可以建立 ...
- 掌握这个Python小技巧,轻松构建cytoscape导入文件
今天小编和大家分享如何借助Python脚本轻松构建cytoscape导入文件.Cytoscape是一个非常适合展示各种相互作用关系的可视化软件. 具体来说就是可以用于蛋白互作网络的展示,miRNA与蛋 ...
- 抗DDOS攻击
2016年10月的某天,“半个美国互联网”都瘫痪了,就是因为遭受了DDoS攻击——Twitter.GitHub.Spotify.Airbnb.Etsy等大量站点都因此受到影响.DNS服务提供商Dyn公 ...
- uiautomator--图像处理
一.图像处理在自动化中使用场景 1)效果类截图 图像处理技术在自动化的场景中很容易使用到.自动化不是万能的,有时候效果类的是无法进行验证的,但是效果类一般会有图像显示,我们可以通过截图对比实现. 2 ...
- Spring注解(环境)
以数据库为例: 引入 c3p0数据源maven坐标 数据库驱动 @Configuration @PropertySource("classpath:/db.config.properties ...
- spark启动
注意在启动spark时候要指定参数 要不就死启动的单机版的 /usr/local/spark-1.5.2-bin-hadoop2.6/bin/spark-shell \ --master spark: ...
- Oracle imp 导入数据出现 ORA-12560
错误如下: D:\software\xfwebdb2015-05-11\autobackup>imp Import: Release 10.2.0.1.0 - Production on 星期三 ...
- 用户iis可以用外网ip访问,用内网访问报错404
如下,没有添加内网ip绑定
- LengthOfLastWord,字符串最后一个子串的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- js插件封装
插件封装原则 1.暴露出来的实例必须只能是一个 2.IIFE包裹 !执行包裹 函数作用域保护 3.实例化方法不要写在函数内 throw这个方法是报错