1.js 支持面向对象编程,但只是基于面向对象,不使用类或者接口。
2.演变 工厂模式-------》构造函数模式----------》原型模式 工厂模式的缺点:
  ①函数内部new ,不太符合 new 函数这种写法 ----解决方法 构造函数
  ②资源浪费,工厂模式中函数中的方法,每一个新函数都会有自己的一个函数------解决方法 原型模式 3.构造函数的缺点:每个成员都无法得到复用,包括函数。
原型链的缺点:对象实例共享所有继承的属性和方法。 因此不适合单独使用,解决办法在子类构造函数内部调用超类的构造函数,就做到了每个实例都有自己的属性 5.最好的办法寄生组合继承:使用原型链继承共享的属性和方法,而通过借用构造函数继承实例的属性。
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title> <script>
function Person(name,sex){
this.name = name;
this.sex= sex;
} Person.prototype.sayName = function(){
alert(this.name);
};
Person.prototype.saySex = function(){
alert(this.sex);
}; function Worker(name,sex,job){
Person.call(this,name,sex); //调用构造函数
this.job = job;
}
/*Worker.prototype = Person.prototype; //引用的写法,这样写改变子类的方法会把父类的方法也改变了*/ for(var attr in Person.prototype){ //这样写就不会了
Worker.prototype[attr] = Person.prototype[attr];
} Worker.prototype.sayJob = function(){
alert(this.job);
}; Worker.prototype.sayName = function(){
alert('123');
}; window.onload = function(){
var person1 = new Person('ww','女');
person1.sayName();
var instance = new Worker('leo','男','soft');
instance.sayName();
instance.sayJob();
}; </script> </head> <body>
</body>
</html>

// JavaScript Document

    /*构造函数*/
function Drag(id){
this.disX = 0;
this.disY = 0;
this.oDiv = document.getElementsByTagName(id)[0];
var _this = this;
this.oDiv.onmousedown = function(ev){
_this.fnDown(ev);
return false;
};
}
Drag.prototype.fnDown = function(ev){
var oEvent = ev || event;
this.disX = oEvent.clientX - this.oDiv.offsetLeft;
this.disY = oEvent.clientY - this.oDiv.offsetTop;
var _this = this;
document.onmouseover = function(ev){
_this.fnMove(ev);
};
document.onmouseup = function(){
_this.fnUp(ev);
};
}
Drag.prototype.fnMove = function(ev){
var ev = ev || event;
this.oDiv.style.left = ev.clientX - this.disX+'px';
this.oDiv.style.top = ev.clientY - this.disY+'px';
}
Drag.prototype.fnUp = function(){
document.onmouseover = document.onmouseup = null;
}
// JavaScript Document

function LimateDrag(id){
Drag.call(this,id); //继承属性
} //继承方法
for(var i in Drag.prototype){
LimateDrag.prototype[i] = Drag.prototype[i]
} LimateDrag.prototype.fnMove = function(ev){
var ev = ev || event;
var l = ev.clientX - this.disX;
var t = ev.clientY - this.disY; if(l < 0){
l = 0;
}else if(l >= document.documentElement.clientWidth-this.oDiv.offsetWidth ){
l = document.documentElement.clientWidth-this.oDiv.offsetWidth;
} if(t < 0){
t = 0;
}else if(t >= document.documentElement.clientHeight-this.oDiv.offsetHeight){
t = document.documentElement.clientHeight-this.oDiv.offsetHeight;
} this.oDiv.style.left = l+'px';
this.oDiv.style.top = t+'px';
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
div{width:100px;height:100px;background:red;position:absolute;left:0;top:0;}
</style>
<script src ='Drag.js'></script>
<script src ='LimateDrag.js'></script> <script>
window.onload = function(){
new LimateDrag('div');
};
</script>
</head> <body>
<div></div>
</body>
</html>

最新文章

  1. Atitit 图像处理—图像形态学(膨胀与腐蚀)
  2. sublime text2 配置代码对齐快捷键
  3. (转载)Bash 中的特殊字符大全
  4. 深圳浩瀚技术有限公司(haohantech)推出的无线移动批发管理PDA解决方案------无线移动POS销售开单系统
  5. jdbc根据实例名 连接 sql server
  6. Gas Station
  7. java小经验
  8. Spring4 customEditors
  9. Java面向对象 IO (三)
  10. 从 Python 快速启动 CGI 服务器
  11. 吴恩达机器学习笔记53-高斯分布的算法(Algorithm of Gaussian Distribution)
  12. Laravel -- 邮箱功能配置问题
  13. 设计模式のIteratorPattern(迭代器模式)----行为模式
  14. 在linux中查看进程占用的端口号
  15. CSS3下的渐变文字效果实现
  16. 基于UML的毕业选题系统建模研究
  17. OkHttp3源码详解(一) Request类
  18. C++ primer第4版 4.25
  19. Report Studio值提示通过prompt宏函数给sql查询传参
  20. Python日期字符串比较

热门文章

  1. Qt unsigned char *与QString之间的相互转换
  2. PP图|QQ图|正态性检验|K-S检验|S-W检验|
  3. Mybatis/ibatis基础知识
  4. 签名旧版的pom文件
  5. JAVA 截图+tess4j识别
  6. 详解服务器性能测试的全生命周期?&mdash;&mdash;从测试、结果分析到优化策略(转载)
  7. Linux sed &amp;&amp; awk
  8. CSS 双飞翼布局
  9. [红日安全]Web安全Day3 - CSRF实战攻防
  10. win10 64位 安装JDK1.8