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面向对象写法的更多相关文章

  1. 原生JS面向对象思想封装轮播图组件

    原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...

  2. 原生js面向对象编程-选项卡(自动轮播)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. 原生js面向对象编程-选项卡(点击)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. screen,client,page三种确定鼠标坐标的区别和原生JS事件写法,区别于Jquery的$.on(x,y);和$.click()

    screenX clientX pageX的区别 screenX:鼠标位置相对于用户屏幕水平偏移量,而screenY也就是垂直方向的,此时的参照点也就是原点是屏幕的左上角. clientX:跟scre ...

  5. 常用原生JS兼容性写法汇总

    1.添加事件方法 addHandler:function(element,type,handler){ if(element.addEventListener){//检测是否为DOM2级方法 elem ...

  6. 原生JS面向对象方法实现万年历

    ###面向对象的方法实现万年历 实现思路:    1.创建构造函数constructor    ```    function Calender(main){         this.current ...

  7. 【JavaScript】两种常见JS面向对象写法

    基于构造函数 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...

  8. js面向对象写法及栈的实现

    function Stack() { this.dataStore = []; this.top = 0; //指向栈顶的位置 this.push = push; this.pop = pop; th ...

  9. 【CSS进阶】原生JS getComputedStyle等方法解析

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

随机推荐

  1. 『NiFi 学习之路』入门 —— 下载、安装与简单使用

    一.概述 "光说不练假把式." 官网上的介绍多少让人迷迷糊糊的,各种高大上的词语仿佛让 NiFi 离我们越来越远. 实践是最好的老师.那就让我们试用一下 NiFi 吧! 二.安装 ...

  2. Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...

  3. ros使用时的注意事项&技巧

    1.rosrun package-name executable-name 比如 rosrun turtlesim turtlesim_node 2.一旦启动roscore后,便可以运行ROS程序了. ...

  4. [转]HBase hbck——检察HBase集群的一致性

    Hbase提供了hbck命令来检查各种不一致问题.hbck的名字仿效了HDFS的fsck命令,后者是一个用于检查HDFS中不一致问题的工具.下面这段非常易懂的介绍出自于hbck的源程序. 检查数据在M ...

  5. c语言URL通过Http下载mp3 格式

    通过http协议下载MP3的关键就是 整块打包,一块一块向文件里面存储.读取的时候用二进制 /***szWebAddr: 页面地址(包含host+addr) szMp3FileName:将要存储文件的 ...

  6. Python MySQL数据库连接模块

    1. MySQLdb只支持在Python 2版本使用MySQLdb是用于Python链接Mysql数据库的接口.a.pip安装 直接使用pip进行安装,在此之前需要安装一些系统依赖包. ● CentO ...

  7. CSV文件导入导出MySQL

    使用SQLyog 工具导入文件数据到MySQL: Excel文件导入导出: 需要驱动:Microsoft Office 2007驱动 导入需要注意的问题:1.Excel里数值列,默认导入会变成浮点型. ...

  8. APIGateway网关安全设计

    Spring Cloud里面有个组件 Zuul网关 网关和 过滤器 拦截器很相似 网关可以实现过滤器 拦截器的功能 而且可以实现Nginx的基本功能 反向代理 负载均衡ribbon Nginx是软负载 ...

  9. HBase 协处理器编程详解,第二部分:客户端代码编写

    实现 Client 端代码 HBase 提供了客户端 Java 包 org.apache.hadoop.hbase.client.coprocessor.它提供以下三种方法来调用协处理器提供的服务: ...

  10. Cacti的基本安装配置

    ////////////////////cacti///////////////////////////常用的监控软件有:cacti.nagios.zabbix等 cacti 重图形.有数据历史.需要 ...