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. 增for语句内容

    #author:leon #"hello world!" for i in range(10): #循环十次,每一次循环赋一个0-9中的数字给i . print("--- ...

  2. rabbitmq的发布确认和事务

    摘要: 介绍confirm的工作机制.使用spring-amqp介绍事务以及发布确认的使用方式.因为事务以及发布确认是针对channel来讲,所以在一个连接中两个channel,一个channel可以 ...

  3. npm国内镜像设置

    http://cnodejs.org/topic/4f9904f9407edba21468f31e

  4. Cookie应用参考

    内容来自imooc.

  5. python的垃圾回收机制 继承的顺序C3算法

    Python垃圾回收    -- 引用计数        -- Python为每个对象维护一个引用计数        -- 当引用计数为0的 代表这个对象为垃圾    -- 标记清除        - ...

  6. Apache 访问控制

    Apache访问控制 通过设置访问控制,可对网站进行权限管理,提高安全性. 参数介绍 <Directory />: 行为对根目录的限制 Options:允许使用控制目录特征的指令.他们包括 ...

  7. shell检查网络出现异常、僵尸进程、内存过低后,自动重启

    #!/bin/bash while : do neterror=$(/bin/netstat -a | grep -cw "CLOSE_WAIT") echo "get ...

  8. [Android]热修复框架AndFix测试说明

    AndFix,全称是Android hot-fix.是阿里开源的一个热补丁框架,允许APP在不重新发布版本的情况下修复线上的bug.支持Android 2.3 到 6.0,并且支持arm 与 X86系 ...

  9. hi.baidu.com 百度流量统计

    在字幕侠的官网访问之后,发现 <meta name="baidu-site-verification" content="3uvZd9Aact" /> ...

  10. selenium学习笔记(xpath和css定位)

    简单的介绍下xpath和css的定位 理论知识就不罗列了 还是利用博客园的首页.直接附上代码: 这个是xpath #!/usr/bin/env python # -*- coding: utf_8 - ...