一、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类
注意事项:
- es6里面class没有提升功能,在es5,用函数模拟可以默认函数提升;
- this的指向(一般this的指向没有问题):
- 矫正this:fn.call(this指向谁,arg1,arg2...);
- fn.apply(this指向谁,[arg1,arg2...])
- 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>