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. hadoop22---wait,notify

    vv wait和notify,是要加syschronized的,是要获取锁的,wait是释放控制权,别的线程就可以执行了,notify和notifyall是通知其他线程执行.

  2. 利用MacBookPro入侵无线网络

    目前无线网络的加密方式主要有WEP,WPA/WPA2.这是最常看到的加密方式,最近由于需要,专门去研究了一下如何入侵无线网络. 1.入侵WEP加密的无线网络 WEP加密方式现在已经很不安全了,因为只要 ...

  3. [BZOJ2342]双倍回文

    对每个大中心暴力找小中心即可. 代码: #include<iostream> #include<cstdio> #include<cstring> #define ...

  4. 并发-ConcurrentHashMap源码分析

    ConcurrentHashMap 参考: http://www.cnblogs.com/chengxiao/p/6842045.html https://my.oschina.net/hosee/b ...

  5. eclipse官网下载

    Provided by IBM Cloud Eclipse IDE for Java Developers http://eclipse.bluemix.net/packages/photon/dat ...

  6. CocoaPods学习系列5——错误集锦

    这篇文章记录使用CocoaPods过程中遇到的一些错误. 1.error:include of non-modular header inside framework module 在自定义类库中,引 ...

  7. Springboot- Spring缓存抽象学习笔记

    Spring缓存作用准备: 1.准备数据(准备一个有数据的库和表/导入数据库文件,准备好表和表里面的数据) 2.创建javaBean封装数据 3.整合MyBatis操作数据库( 这里用MyBatis) ...

  8. UIElement.IsMouseCaptured属性的应用

    一个只读属性,该值描述了此元素是否捕获到了鼠标,如果该值为true,则说明此元素捕获到了鼠标:否则,未捕获到(例如:当鼠标进入到一个Button的可视化范围之内,当Button按钮外观效果发生了变化时 ...

  9. 5.6 WebDriver API实例讲解(41-50)

    41.操作Web页面的滚动条 (1)滑动页面的滚动条到页面的最下面. (2)滑动页面的滚动条到页面的某个元素. (3)滑动页面的滚动条向下移动某个数量的像素. package apiSample; i ...

  10. 在Windows Server 2012上安装SharePoint 2010 SP1

    现在很多企业的系统都开始用上了Windows 2012,最近公司需要建立一个门户系统,一开始就想到了微软的SharePoint2010,这玩意确实非常强悍,2008已经褪去,当然直接在Windows ...