继承 yu 拖拽

继承

一、继承的定义:

1、在一个类基础上定义一个新类叫继承,原有的类叫父类,新生成的类叫子类。
2、一个父类可以有多个子类,一个子类只能有一个父类。

二、继承的特点:
1、子类拥有父类的属性和方法
2、子类可以有自己新的属性和方法
3、子类可以重写(覆盖)父类的方法
4、可以声明父类,创建子类

写一个简单的对象

function Person(name,age) {
    this.name = name;
    this.age = age;
}
Person.prototype.showName = function () {
    console.log("名字:" + this.name);
};
Person.prototype.showAge = function () {
  console.log("年龄:" + this.age);
};

拖拽

首先写出我们要拖拽的物体

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <style>
        div {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }

        #box2 {
            background: green;
        }

        #box3 {
            background: blue;
        }
    </style>
  
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="box3"></div>

    <script src="DragBox.js"></script>
    <script>
        function  DragBoxText(boxId) {
            DragBox.call(this,boxId)
        }
        DragBoxText.prototype = new DragBox();
        DragBoxText.prototype.move = function  (x,y) {
            DragBox.prototype.move.call(this,x,y);
            this.box.innerHTML = x+"px"+y+"px"
        }       
        function  DragBoxs(boxId) {
            DragBox.call(this,boxId)
        }
        DragBoxs.prototype = new DragBox();
        DragBoxs.prototype.move = function  (x,y) {
            DragBox.prototype.move.call(this,x,y);
            if(x<=0 && y<=0){
                this.box.style.top = 0+"px"
                this.box.style.left = 0+"px"
            }else if (x<=0 ) {
                this.box.style.left = 0+"px"
                
            }else if(y<=0){
                this.box.style.top = 0+"px"
            }           
        }
             //
        new DragBoxText("box3");
        new DragBoxs("box2");
        new DragBox("box1");
        
    </script>

    </body>
</html>

引入我们的JS文件


function DragBox (boxId) {
    if (boxId == undefined) {
        return;
    }
    this.box = document.getElementById(boxId);
    var self = this;
    this.box.onmousedown = function  (e) {
        e.preventDefault();
        self.detaX = e.clientX-self.box.offsetLeft;
        self.detaY = e.clientY-self.box.offsetTop;
        self.start()
        document.onmouseup = function  () {
            self.stop()
        }
      }
    }
    DragBox.prototype.start = function  () {
        var self = this;
        document.onmousemove = function  (e) {
            var x = e.clientX - self.detaX;
            var y = e.clientY - self.detaY;
            self.move(x,y)
        }       
    }
    DragBox.prototype.move = function  (x,y) {
        var self = this;
        self.box.style.left = x+"px";
        self.box.style.top = y+"px"
    }
    DragBox.prototype.stop = function  () {
        document.onmousemove = null
    }

这里call的用法
call和apply,它们的作用都是将函数绑定到另外一个对象上去运行
两者的格式和参数定义:
call( this,var1,var2,… ); // 当前对象,参数列表(形参)
apply(this, [var1,var2,...] ); // 当前对象,必须是数组
这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • this 相关问题 问题1: apply、call 有什么作用,什么区别 Javascript的每个Functio...
    Maggie_77阅读 611评论 0 0
  • 一、数组 1、数组定义 数组就是一个键值对组成的语言结构,键类似于酒店的房间号,值类似于酒店房间里存储的东西。 $...
    空谷悠阅读 814评论 4 11
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,764评论 18 399
  • Ahoh阅读 158评论 0 0
  • 昨天看完《危城》,好多感想。刚好接触到简书,把自己心中的点滴记录下来,分享给大家,讨论下,若无人欣赏,至少以后...
    NoNoAnyway阅读 901评论 0 1