es6中的class与继承

注意项:

1.在类中声明方法的时候,千万不要给该方法加上function关键字

2.方法之间不要用逗号分隔,否则会报错

由下面代码可以看出类实质上就是一个函数。类自身指向的就是构造函数。所以可以认为ES6中的类其实就是构造函数的另外一种写法!

console.log(typeof Person);//function

console.log(Person===Person.prototype.constructor);//true

以下代码说明构造函数的prototype属性,在ES6的类中依然存在着。

console.log(Person.prototype);//输出的结果是一个对象

实际上类的所有方法都定义在类的prototype属性上。代码证明下:

Person.prototype.say=function(){//定义与类中相同名字的方法。成功实现了覆盖!

return “我是来证明的,你叫” + this.name+“今年”+this.age+“岁了”;

}

var obj=new Person(“kity”,88);

console.log(obj.say());//我是来证明的,你叫kity今年88岁了

当然也可以通过prototype属性对类添加方法。如下:

Person.prototype.addFn=function(){

return “我是通过prototype新增加的方法,名字叫addFn”;

}

var obj=new Person(“kity”,88);

console.log(obj.addFn());//我是通过prototype新增加的方法,名字叫addFn

还可以通过Object.assign方法来为对象动态增加方法

Object.assign(Person.prototype,{

getName:function(){

return this.name;

},

getAge:function(){

return this.age;

}

})

var obj=new Person(“kity”,88);

console.log(obj.getName());//kity

console.log(obj.getAge());//88

constructor方法是类的构造函数的默认方法,通过new命令生成对象实例时,自动调用该方法。

class Box{

constructor(){

console.log(“啦啦啦,今天天气好晴朗”);//当实例化对象时该行代码会执行。

}

}

var obj=new Box();

constructor方法如果没有显式定义,会隐式生成一个constructor方法。所以即使你没有添加构造函数,构造函数也是存在的。constructor方法默认返回实例对象this,但是也可以指定constructor方法返回一个全新的对象,让返回的实例对象不是该类的实例。

class Desk{

constructor(){

this.aa=“我是一只小小小小鸟!哦”;

}

}

class Box{

constructor(){

return new Desk();// 这里没有用this哦,直接返回一个全新的对象

}

}

var obj=new Box();

console.log(obj.aa);//我是一只小小小小鸟!哦

constructor中定义的属性可以称为实例属性(即定义在this对象上),constructor外声明的属性都是定义在原型上的,可以称为原型属性(即定义在class上)。hasOwnProperty()函数用于判断属性是否是实例属性。其结果是一个布尔值, true说明是实例属性,false说明不是实例属性。in操作符会在通过对象能够访问给定属性时返回true,无论该属性存在于实例中还是原型中

class Box{

constructor(num1,num2){

this.num1 = num1;

this.num2=num2;

}

sum(){

return num1+num2;

}

}

var box=new Box(12,88);

console.log(box.hasOwnProperty(“num1”));//true

console.log(box.hasOwnProperty(“num2”));//true

console.log(box.hasOwnProperty(“sum”));//false

console.log(“num1” in box);//true

console.log(“num2” in box);//true

console.log(“sum” in box);//true

console.log(“say” in box);//false

类的所有实例共享一个原型对象,它们的原型都是Person.prototype,所以proto属性是相等的

class Box{

constructor(num1,num2){

this.num1 = num1;

this.num2=num2;

}

sum(){

return num1+num2;

}

}

//box1与box2都是Box的实例。它们的__proto__都指向Box的prototype

var box1=new Box(12,88);

var box2=new Box(40,60);

console.log(box1.proto===box2.proto);//true

由此,也可以通过proto来为类增加方法。使用实例的proto属性改写原型,会改变Class的原始定义,影响到所有实例,所以不推荐使用!

class Box{

constructor(num1,num2){

this.num1 = num1;

this.num2=num2;

}

sum(){

return num1+num2;

}

}

var box1=new Box(12,88);

var box2=new Box(40,60);

box1.proto.sub=function(){

return this.num2-this.num1;

}

console.log(box1.sub());//76

console.log(box2.sub());//2

class不存在变量提升,所以需要先定义再使用。因为ES6不会把类的声明提升到代码头部,但是ES5就不一样,ES5存在变量提升,可以先使用,然后再定义。

//ES5可以先使用再定义,存在变量提升

new A();

function A(){

}

//ES6不能先使用再定义,不存在变量提升 会报错

new B();//B is not defined

class B{

}

[](()ES5中的继承 (组合继承:原型链继承 + 借用构造函数)

原型链继承:

父类的实例作为子类的原型

function Persion(name,age){

this.name=name;

this.age=age;

this.say=function(){

conso 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 le.log(${this.name} ${this.age});

}

}

Persion.prototype.test=function(){

console.log(“我是原型上的方法”);

}

//子类

function Women(){

this.sex=‘女’

}

Women.prototype=new Persion(‘kity’,18);//父类的实例作为子类的原型

var one=new Women(‘tom’,20);

one.say();//kity 18 子类实例,不能向父类构造函数中传参数

one.test();// 可以调用原型方法 我是原型上的方

借用构造函数继承:

在子类内,使用call()调用父类方法,并将父类的this修改为子类的this.相当于是把父类的实例属性复制了一份放到子类的函数内.

//父类

function Persion(name,age){

this.name=name;

this.age=age;

this.say=function(){

console.log(${this.name} ${this.age});

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值