ES6新特性 类(class)详解

传统的javascript中只有对象,没有类的概念。它是基于原型的面向对象语言。原型对象特点就是将自身的属性共享给新对象。这样的写法相对于其它传统面向对象语言来讲,很有一种独树一帜的感脚!非常容易让人困惑!

1. class类与构造函数对比
1.1 构造函数实现

如果要生成一个对象实例,需要先定义一个构造函数,然后通过new操作符来完成。

// 函数名和实例化构造名相同且大写(非强制,但这么写有助于区分构造函数和普通函数)
function Person(name,age) {
  this.name = name;
  this.age = age;
}
Person.prototype.say = function(){
  return "我的名字叫" + this.name + ",今年" + this.age + "岁了";
}
var obj = new Person("小明",12); // 通过构造函数创建对象,必须使用new 运算符
console.log(obj.say()); // 我的名字叫小明,今年12岁了

构造函数生成实例的执行过程:
1.当使用了构造函数,并且new 构造函数(),后台会隐式执行new Object()创建对象;
2.将构造函数的作用域给新对象,(即new Object()创建出的对象),而函数体内的this就代表new Object()出来的对象。
3.执行构造函数的代码。
4.返回新对象(后台直接返回);

1.2 class类实现
class Person{ // 定义了一个名字为Person的类
  constructor(name,age){ // constructor是一个构造方法,用来接收参数
    this.name = name; // this代表的是实例对象
    this.age = age;
  }
  say(){ // 这是一个类的方法,注意千万不要加上function
    return "我的名字叫" + this.name + ",今年"+this.age+"岁了";
  }
}
var obj = new Person("小明",12);
console.log(obj.say()); // 我的名字叫小明,今年12岁了

注意项:
1.在类中声明方法的时候,千万不要给该方法加上function关键字;
2.方法之间不要用逗号分隔,否则会报错;

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

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

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

以上代码说明构造函数的prototype属性,在ES6的类中依然存在着。
实际上类的所有方法都定义在类的prototype属性上。代码证明下

Person.prototype.say=function(){	// 定义与类中相同名字的方法。成功实现了覆盖!
  return "哈哈,我叫" + this.name + ",今年"+ this.age + "岁了";
}
var obj=new Person("小艺",20);
console.log(obj.say()); // 哈哈,我叫小艺,今年20岁了
2. class类添加方法
  1. prototype 属性对类添加方法
Person.prototype.addFn=function(){
  return "我是通过prototype新增加的方法,名字叫addFn";
}
var obj=new Person("小明",12);
console.log(obj.addFn()); // 我是通过prototype新增加的方法,名字叫addFn
  1. Object.assign 方法对类动态增加方法
Object.assign( Person.prototype,{
  getName:function(){
    return this.name;
  },
  getAge:function(){
    return this.age;
  }
})
var obj = new Person("小强",10);
console.log(obj.getName()); // 小强
console.log(obj.getAge()); // 10
3. constructor方法

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

class Box{
  constructor(){
    console.log("啦啦啦,今天是元宵节"); // 当实例化对象时该行代码会执行。
  }
}
var obj = new Box();

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

class Desk{
  constructor(){
    this.wolf = "我是灰太狼";
  }
}
class Box{
  constructor(){
    return new Desk(); // 这里没有用this哦,直接返回一个全新的对象
  }
}
var animal = new Box();
console.log(animal.wolf); // 我是灰太狼

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

类的所有实例共享一个原型对象,所以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(2,4);
var box2 = new Box(1,5);
box1.__proto__.sub=function(){
  return this.num2 - this.num1;
}
console.log(box1.sub()); // 2
console.log(box2.sub()); // 4
4. class不存在变量提升

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

// ES5可以先使用再定义,存在变量提升
new A();
function A(){

}
// ES6不能先使用再定义,不存在变量提升 会报错
new B(); // B is not defined
class B{

}

参考文章(侵删)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明天也要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值