ES6_09、class

一、js构造函数的使用回顾

// 构造函数
	function Person(name,age){
		this.name=name;
		this.age=age;
		this.run=function(){
			return `${this.name},跑起来!`
		}
	}
	// 添加方法

	// 写法一
	// Person.prototype.showName=function(){
	// 	return `姓名为:${this.name}`
	// }
	// Person.prototype.showAge=function(){
	// 	return `年龄为:${this.age}`
	// }

	// 写法二:
	Object.assign(Person.prototype,{
		showName(){
			return `姓名为:${this.name}`
		},
		showAge(){
			return `年龄为:${this.age}`
		}
	})

	let p=new Person('张三',25);
	console.log(p.showName(),p.showAge(),p.run());//姓名为:张三 年龄为:25 张三,跑起来!

二、class类

注意事项:

  1. es6里面class没有提升功能,在es5,用函数模拟可以默认函数提升;
  2. this的指向(一般this的指向没有问题):
    1. 矫正this:fn.call(this指向谁,arg1,arg2...);
    2. fn.apply(this指向谁,[arg1,arg2...])
    3. fn.bind()
class Person{
    constructor(name,age){
        this.name=name;
        this.age=age;
    }
    show(){
        console.log(this.name,this.age);
    }
};
let p=new Person('张三',27);
p.show();

三、class里面的取值函数(getter),存值函数(setter)

class Person{
    constructor(){

    }
    get aaa(){
        return 'aaa属性返回'
    }
    set aaa(val){
        console.log(val,'设置的值')
    }
}
let p=new Person();
p.aaa='123';
console.log(p.aaa)

四、静态方法

静态方法就是类上面的方法

class Person{
    constructor(name,age){
        this.name=name;
        this.age=age;

    }
    showName(){
        console.log(`名字:${this.name},年龄:${this.age}`)
    }
    //静态方法
    static aaa(){
        console.log('这是类上面的静态方法')
    }
}
let p=new Person('ty',15);
p.showName();
Person.aaa();//调用静态方法

五、类的继承 extends

1、简单的继承方法:

// 父类
class Person{
	constructor(name){
		this.name=name;
	}
	showName(){
		return `名字为:${this.name}`
	}
}
// 子类  继承
class Student extends Person{
	constructor(name,skill){
		super(name);//子类需要执行的方法
		this.skill=skill;
	}
	showSkill(){
		return `技能是:${this.skill}`
	}
}

let s=new Student('ty','逃学');
console.log(s.showName());
console.log(s.showSkill());

 2、子类里面有和父类一样的方法的处理方式

子类与父类方法同名,当调用的时候父类的方法就不会执行,可以使用super.方法名()来执行

//父类
class Person{
	constructor(name){
		this.name=name;
	}
	showName(){
		return `【父类执行】名字为:${this.name}`
	}
}
// 子类
class Student extends Person{
	constructor(name,skill){
		super(name);
		this.skill=skill;
	}
	showSkill(){
		return `【子类执行】技能是:${this.skill}`
	}
	showName(){
		let f=super.showName();//执行父类的方法
		console.log(f);
		return `【子类执行】名字为:${this.name}`
	}
}

let s=new Student('zs','上学');
console.log(s.showName());
console.log(s.showSkill());

 六、案例 拖拽的使用

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>类的继承实现拖拽</title>
</head>
<style>
	body{
		width: 100%;
		height: 400px;
		border:solid 1px pink;
		padding:0;
		margin:0;
	}
	div.box{
		width: 50px;
		height: 50px;
		border:solid red 1px;
		background-color:pink;
		position:absolute;
		top:0;
		cursor:pointer;
	}
	.left{
		left:0;
	}
	.right{
		right:0;
	}
</style>
<body >
	<div id="div1" class="box left">DIV1</div>
	<div id="div2" class="box right">DIV2</div>
</body>
</html>
<script>
	class Drag{
		constructor(id){
			this.oDiv=document.querySelector(id);
			this.disX=0;
			this.disY=0;
			this.init();
		}
		init(){
			this.oDiv.onmousedown=function(ev){
				// this.disx=ev.clientX-this.oDiv.offsetLeft-25;
				// this.disY=ev.clientY-this.oDiv.offsetTop-25;
				document.onmousemove=this.fnMove.bind(this);
				document.onmouseup=this.fnUp.bind(this);
				return false;
			}.bind(this)//修改this指向s
		}
		fnMove(ev){
			// console.log(ev.clientX,this.disX,ev.clientY,this.disY)
			
			this.oDiv.style.left=ev.clientX-25+'px';
			this.oDiv.style.top=ev.clientY-25+'px';
		}
		fnUp(ev){
			document.onmousemove=null;
			document.onmouseup=null;

		}
	}
	class limitDrag extends Drag{
		fnMove(ev){
			super.fnMove(ev);
			if(this.oDiv.offsetLeft<=0){
				this.oDiv.style.left=0;
			}
			if(this.oDiv.offsetTop<=0){
				this.oDiv.style.top=0;
			}
			if(this.oDiv.offsetLeft>document.body.clientWidth-50){
				this.oDiv.style.left=document.body.clientWidth-50 +'px';
			}
			console.log(this.oDiv.offsetTop,document.body.offsetHeight)
			if(this.oDiv.offsetTop>document.body.offsetHeight-50){
				console.log('2222rrrrrs')
				this.oDiv.style.top=document.body.offsetHeight-50 +'px';
			}
		}	
	}
	new Drag('#div1');
	new limitDrag("#div2");
</script>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值