1. 对象属性及方法

  • 创建对象的方式
<script>
//创建对象的方式一
var obj = {};
//创建对象的方式一
var obj = new Object();
</script>
  • 挂载在对象上的变量叫对象的属性;挂载在对象上的函数叫对象的方法
    obj.name = "David";
obj.say = function () {
alert(this.name)
}
obj.say();

2. 创建对象的发展

  • 普通创建模式
    var obj1 = new Object();
obj1.name = "David";
obj1.say = function () {
alert(this.name)
}
obj1.say();
var obj2 = new Object();
obj2.name = "Mike";
obj2.say = function () {
alert(this.name)
}
obj2.say();
  • 工厂模式
    function create(name,age){
var obj = new Object();
obj.name = name;
obj.age = age;
obj.say = function () {
alert("姓名:"+ this.name + "年龄:" + this.age);
}
return obj;
}
var obj1 = create("David",22);
obj1.say();
var obj2 = create("Mike",18);
obj2.say();
  • 构造函数模式

    •   obj1.say != obj2.say,同样的方法占用了两块内存
    function create(name,age){
obj.name = name;
obj.age = age;
obj.say = function () {
alert("姓名:"+ this.name + "年龄:" + this.age);
}
}
var obj1 = new create("David",22);
obj1.say();
var obj2 = new create("Mike",18);
obj2.say();
  • 原型模式

    •   obj1.say = obj2.say,同样的方法占用了一块内存,即两个对象共用了一个方法
    •   不同的属性放在构造函数里,相同的方法放在原型对象上
    •   原型对象也是对象
    function create(name,age) {
obj.name = name;
obj.age = age;
}
create.prototype.say = function () {
alert("姓名:"+ this.name + "年龄:" + this.age);
}
var obj1 = new create("David",22);
obj1.say();
var obj2 = new create("Mike",18);
obj2.say();

3. 实现tab选项卡

  • 面向过程实现tab选项卡

css代码

        #big div{
border: 1px solid #000;
width: 200px;
height: 200px;
display: none;
}
#big .block{
display: block;
}
input.active{
background-color: red;
}

html代码

<div id = "big">
<input type = "button" value ="选项一" class = "active"/>
<input type = "button" value = "选项二"/>
<input type = "button" value = "选项三"/>
<div class = "block">我是内容一</div>
<div>我是内容二</div>
<div>我是内容三</div>
</div>

js代码

    window.onload = function(){
var big = document.getElementById("big");
var inputs = big.getElementsByTagName("input");
var divs = big.getElementsByTagName("div");
for(var i = 0;i < inputs.length;i++){
inputs[i].index = i;
inputs[i].onclick = function(){
for(var j = 0;j < inputs.length;j++){
inputs[j].className = "";
divs[j].className = "";
}
this.className = "active";
divs[this.index].className = "block";
}
}
}
  • 面向对象实现tab选项卡

css代码

        #big1 div,#big2 div{
width: 200px;
height: 200px;
display: none;
border: 1px solid #000;
}
#big1 .block,#big2 .block{
display: block;
}
input.active{
background-color: red;
}

html代码

<div id = "big1">
<input type = "button" value = "选项一" class = "active"/>
<input type = "button" value = "选项二"/>
<input type = "button" value = "选项三"/>
<div class = "block">我是内容一</div>
<div>我是内容二</div>
<div>我是内容三</div>
</div>
<div id="big2">
<input type = "button" value = "选项一" class = "active"/>
<input type = "button" value = "选项二"/>
<input type = "button" value = "选项三"/>
<div class = "block">我是内容一</div>
<div>我是内容二</div>
<div>我是内容三</div>
</div>

js代码

    window.onload = function(){
var tab1 = new Tab("big1");
var tab2 = new Tab("big2");
}
function Tab(id){
var big = document.getElementById(id);
this.inputs = big.getElementsByTagName("input");
this.divs = big.getElementsByTagName("div");
var that = this;
for(var i = 0;i < this.inputs.length;i++){
this.inputs[i].index = i;
this.inputs[i].onclick = function(){
that.show(this);
}
}
}
Tab.prototype.show = function(obtn){
for(var j = 0;j < this.inputs.length;j++){
this.inputs[j].className = "";
this.divs[j].className = "";
}
obtn.className = "active";
this.divs[obtn.index].className = "block";
}

4. 引用

  • 引用赋值
    // 引用赋值,共用一块内存空间
var arr1 = [1,2,3,4];
var arr2 = [];
for(var i = 0;i < arr1.length;i++){
arr2[i] = arr1[i];
}
arr2.push(5);
  • 继承基本语法

    •   继承属性
  • function A(){ this.color = "black"; } A.prototype.show = function(){ alert("Hello!"); } function B(){ A.call(this); } var b = new B(); console.log(b.color);//通过callnew出来的b有color属性
    •   继承方法
    function A(){
this.color = "black";
}
A.prototype.show = function(){
alert("Hello!");
}
function B(){
A.call(this);
}
for(var i in A.prototype){
B.prototype[i] = A.prototype[i];
}
var b = new B();
b.show();
  • 拖拽案例

html代码

<div id = "box" style="width: 200px;height: 200px;background-color: red;position: absolute;">
</div>
    •   原生js实现拖拽
  window.onload = function(){
var box = document.getElementById("box");
box.onmousedown = function(event){//鼠标按下事件
var ev = event||window.event;
var divX = ev.clientX - box.offsetLeft;
var divY = ev.clientY - box.offsetTop;
document.onmousemove = function(event){//鼠标移动事件
var ev = event || window.event;
box.style.left = ev.clientX - divX + "px";
box.style.top = ev.clientY - divY + "px";
}
document.onmouseup = function(){//鼠标抬起事件
document.onmouseup = null;
document.onmousemove = null;
}
}
}
    •   面向对象实现拖拽
   window.onload = function(){
new Drag("box");
}
function Drag(id){
this.divX = null;
this.divY = null;
this.box = document.getElementById(id);
var that = this;
this.box.onmousedown = function(event){
that.down(event);
}
}
Drag.prototype.down = function(){
var ev = event||window.event;
this.divX = ev.clientX - this.box.offsetLeft;
this.divY = ev.clientY - this.box.offsetTop;
var that = this;
document.onmousemove = function(event){
that.move(event);
}
document.onmouseup = function(){
that.up();
}
}
Drag.prototype.move = function(){
var ev = event||window.event;
this.box.style.left = ev.clientX - this.divX + "px";
this.box.style.top = ev.clientY - this.divY + "px";
}
Drag.prototype.up = function(){
document.onmouseup = null;
document.onmousemove = null;
}
    •   继承实现拖拽

html代码

<div id = "box1" style = " width: 200px;height: 200px;background-color: red; position: absolute;">
</div>
<div id = "box2" style = "width: 200px;height: 200px;background-color: red;position: absolute;">
</div>

js代码

window.onload = function(){
new Drag("box1");
new LimitDrag("box2");
}
function Drag(id){
this.divX = null;
this.divY = null;
this.box = document.getElementById(id);
var that = this;
this.box.onmousedown = function(event){
that.down(event);
}
}
Drag.prototype.down = function(){
var ev = event||window.event;
this.divX = ev.clientX - this.box.offsetLeft;
this.divY = ev.clientY - this.box.offsetTop;
var that = this;
document.onmousemove = function(event){
that.move(event);
}
document.onmouseup = function(){
that.up();
}
}
Drag.prototype.move = function(){
var ev = event||window.event;
this.box.style.left = ev.clientX - this.divX + "px";
this.box.style.top = ev.clientY - this.divY + "px";
}
Drag.prototype.up = function(){
document.onmouseup = null;
document.onmousemove = null;
}
function LimitDrag(id){
Drag.call(this,id);
}
for(var i in Drag.prototype){
LimitDrag.prototype[i] = Drag.prototype[i];
}
LimitDrag.prototype.move = function(event){
var ev = event||window.event;
var posX = event.clientX - this.divX;
var posY = event.clientY - this.divY;
if(posX < 0){
posX = 0;
}
if(posX > document.documentElement.clientWidth - this.box.offsetWidth){
posX = document.documentElement.clientWidth - this.box.offsetWidth;
}
if(posY < 0){
posY = 0;
}
if(posY > document.documentElement.clientHeight - this.box.offsetHeight){
posY = document.documentElement.clientHeight - this.box.offsetHeight;
}
this.box.style.left = posX + "px";
this.box.style.top = posY + "px";
}

5. 原型链

JavaScript 高级之面向对象的更多相关文章

  1. javascript高级特性(面向对象)

    javascript高级特性(面向对象): * 面向对象: * 面向对象和面向过程的区别: * 面向对象:人就是对象,年龄\性别就是属性,出生\上学\结婚就是方法. * 面向过程:人出生.上学.工作. ...

  2. Javascript高级程序设计——面向对象小结

    ECMAScript支持面向对象编程,对象可以在代码执行时创建,具有动态扩展性而非严格意义上的实体. 创建对象方法: 工厂模式:简单的函数创建引用类型 构造函数模式:可以创建自定义引用类型,可以想创建 ...

  3. Javascript高级程序设计——面向对象之理解对象

    在面向对象语言中都有类的概念,通过类来创建具有属性和方法的对象.而ECMAScript中没有类的概念,ECMAScript中定义了对象:无需属性的集合,其属性值可以包含基本值.对象.或者函数. 在Ja ...

  4. JavaScript高级与面向对象

    对象:任何事物都可以看作是对象. 1.面向对象与面向过程的概念 面向过程:凡是自己亲力亲为,自己按部就班的解决现有问题. 面向对象:自己充当一个指挥者的角色,指挥更加专业的对象帮我解决问题. 联系:面 ...

  5. 2020/06/06 JavaScript高级程序设计 面向对象的程序设计

    ECMAScript虽然是一种面向对象的语言,但是他没有类的概念.所以他的对象也与其他语言中的对象有所不同. ECMA-262定义对象:一组没有特定顺序的值. 6.1 理解对象 创建对象的方法: 1. ...

  6. Javascript高级程序设计——面向对象之实现继承

    原型链: 构造函数中都有一个prototype属性指针,这个指针指向原型对象,而创建的实例也有指向这个原型对象的指针__proto__.当实例查找方法时先在实例上找,找不到再通过__proto__到原 ...

  7. Javascript高级程序设计——面向对象之创建对象

    对象创建方法: 工厂方法 构造函数模式 原型模式 组合构造函数和原型模式 寄生构造函数模式 问题构造函数模式 工厂模式: function Person(name, age){ var obj = n ...

  8. Javascript高级篇-面向对象的特性

    一.创建对象 1.1初始化器 var any={ name:"some", age:10, action:function(){ alert(this.name+":&q ...

  9. javascript高级特性

    01_javascript相关内容02_函数_Arguments对象03_函数_变量的作用域04_函数_特殊函数05_闭包_作用域链&闭包06_闭包_循环中的闭包07_对象_定义普通对象08_ ...

随机推荐

  1. LeetCode41.缺失的第一个正数 JavaScript

    给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...

  2. 为什么我不再用 .NET 框架(网摘)

    觉得好就拿过来收藏了,保留出处链接-凌风 2017年08月23日 14:51:32 hisense_大致若愚 阅读数:9355 .NET平台很棒.真的很棒.直到它不再那么棒.我为什么不再用.NET?简 ...

  3. md5,md2加密加盐

    数组是没有重写object的toString()方法.byte[].toString()

  4. MongoDB DBA 实践5-----复制集集群的数据同步和故障转移

    (1)复制集集群的数据同步 1>主节点数据库test,在其中goods集合中加入一个文档. 2>在副节点中查看 注意:SECONDARY是不允许读写的,要使用rs.slaveOk()获得读 ...

  5. 遍历collection是否会出现重复遍历?

    在处理一次线上问题时,需要遍历一张玩家信息表,看单个account是否存在多个entity.使用aid_playerid_dict建立aid到playerid的映射,遍历过程中,发现同一个aid会出现 ...

  6. laravel5.5源码笔记(五、Pipeline管道模式)

    Pipeline管道模式,也有人叫它装饰模式.应该说管道是装饰模式的一个变种,虽然思想都是一样的,但这个是闭包的版本,实现方式与传统装饰模式也不太一样.在laravel的源码中算是一个比较核心的设计模 ...

  7. 《Nginx高性能Web服务器》系列分享专栏

    <Nginx高性能Web服务器>系列分享专栏 [作者:Poechant] Nginx是目前最流行的基于BSD-like协议.轻量级.高性能的HTTP服务器.反向代理服务器和电子邮件(SMT ...

  8. 20155317 2016-2017-2《Java程序设计》课程总结

    20155317 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 新玮的首发博客:对师生关系的期望. C语言与java 20155317 王新玮第二次:语言掌握调查 ...

  9. 20155323 第三次实验 敏捷开发与XP实践

    20155323 第三次实验 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器 ...

  10. CF 1025 D. Recovering BST

    D. Recovering BST http://codeforces.com/contest/1025/problem/D 题意: 给出一个连续上升的序列a,两个点之间有边满足gcd(ai ,aj) ...