原生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 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...
随机推荐
- Kattis - fence2【二分法】
Kattis - fence2[二分法] 题意 有一个农夫需要建造一个 N - 1 米长的篱笆,需要 N 根柱子,然后有 K 根 柱子 需要将这 K 根柱子 切成 N 段 然后 要尽量保证这 N 段柱 ...
- maven入门学习(一)
一.maven介绍 1.软件开发中我们为什么要使用maven呢?(纯属个人体会观点,如有错误,敬请指正) (1)其一,企业岗位需求,目前的IT开发招聘岗位上,基本都要求会使用maven. ...
- JavaScript创建类的三种方式
//第一种 创建类方法. // 用方法模拟 构造函数. function classobj() { this.name = 'xiaoming'; } classobj.text = 'text'; ...
- Spark机器学习2·准备数据(pyspark)
准备环境 anaconda nano ~/.zshrc export PATH=$PATH:/anaconda/bin source ~/.zshrc echo $HOME echo $PATH ip ...
- Android 电池关机充电
android 电池(一):锂电池基本原理篇 android 电池(二):android关机充电流程.充电画面显示 android 电池(三):android电池系统 android电池(四):电池 ...
- 【Flask】Flask Restful api
### 安装: Flask-Restful需要在Flask 0.8以上的版本,在Python2.6或者Python3.3上运行.通过pip install flask-restful即可安装. ### ...
- 行列转换文本处理--awk xargs 回顾
awk 数组回顾: 9.1 数组 举例:统计当前主机上每一个TCP连接状态以及每种连接状态的数目[非常实用] # netstat -tan | awk '/^tcp/{STATE[$NF]++}END ...
- Ubuntu系统Anaconda下载安装与切换源教程【转】
本文转载自:https://blog.csdn.net/qq_36268036/article/details/81517893 1. 下载安装: 这里选用国内清华大学的Tuna开源软件镜像站作为演示 ...
- idea通过springboot初始化器新建项目
1.通过初始化器新建项目,勾选后 对应生成的pom文件 以及生成的包路径 2.生成项目后点击稍后弹出的自动自动导入maven工程的改变,当pom中有依赖改变时会自动刷新导入依赖 3.删除自动生成项目的 ...
- POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找
2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...