ES6 class基本语法 及其 基本使用

4 篇文章 0 订阅
3 篇文章 0 订阅

ES6 class基本语法 及其 基本使用

一、class的简介
class可以看作是一个语法糖,目的是让写出来的代码更加简洁明了。
class语法创建实例对象与传统方法的差异:
传统方法通过构造函数实现:

function square(height,width){
    this.height = height;
    this.width = width;
}
square.prototype.area = function(){
    return this.height * this.width;
}
let a_square = new square(50,100);
console.log(a_square,a_square.area());
--------------------------------------------log
square {height: 50, width: 100} 5000

class语法创建实例对象

class square {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
    area(){
        return this.height * this.width;
    }
}


var a_square = new square(50,100)
console.log(a_square,a_square.area());
--------------------------------------------log
square {height: 50, width: 100} 5000

由两图可以明显看出,class创建实例更偏向于面向对象的写法,辨识度更高,更容易理解。
滴滴: 类上定义的方法,实例会继承。即实例.fun()===类.prototype.fun()。
如下:

a_square.area === square.prototype.area
true

constructor方法
class会默认创建constructor方法并返回实例对象(即this),当然,是在你未定义constructor的时候。
所以你可以在constructor方法内部定义实例需要继承的属性及属性值。
getter和setter 取值函数和存值函数
简单说getter和setter作用:
如果一个class定义的有getter和setter——class创建实例后,想要访问实例的某个属性,会先调用getter方法;想要设置实例的某个属性,会先调用setter方法。

class square {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
    get height(){
        console.log('调用getter')
        return 10
    }
    set height(val){
        console.log('调用setter', val)
    }
}
var a_square = new square()
a_square.height

---调用setter undefined
---调用getter
---10
a_square.height = 1000
---调用setter 1000
---1000

属性和类的表达式模式

let name = "squareNum1"
const dis = class square {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
    [name](){
        console.log('squareNum1')
    }
}
var disExample = new dis()
disExample.squareNum1()
---squareNum1          //dis变量接收类直接new实例
---undefined
dis.squareNum1
---undefined
dis.squareNum1()
----VM1376:1 Uncaught TypeError: dis.squareNum1 is not a -----function
---    at <anonymous>:1:5
---(anonymous) @ VM1376:1
dis.prototype.squareNum1()   //类上的方法需要在原型链上找
---squareNum1
---undefined

二、static静态方法
简述static:创建class的时候,可以通过static创建无法被实例继承的方法。只能使用类调用该方法去执行。

class square {
    static getName(){
        return 'static静态方法'
    }
    getAge(){
        return '非静态方法'
    }
}
var a_square = new square()
a_square.getAge()
---"非静态方法"
square.getName()
---"static静态方法"

实例属性的其他写法
直接在class类的最顶端定义属性及属性值
之前说的是在constructor方法中定义属性及属性值

class square{
    name = 'zhuo'
    area(){
        console.log('调用area')
    }
}
var a_square = new square()
a_square.name
"zhuo"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ES6 class是一种用于定义对象的语法糖,它可以更方便地定义类和继承。 1. 定义类 要定义一个类,可以使用class关键字,后面跟类名,类名通常首字母大写。 ```javascript class Person { constructor(name, age) { this.name = name; this.age = age; } sayHello() { console.log(`Hello, my name is ${this.name}.`); } } ``` 上面的代码定义了一个名为Person的类,它有一个构造函数和一个方法sayHello。构造函数接受两个参数name和age,用于初始化对象的属性。sayHello方法用于输出对象的名字。 2. 创建对象 要创建一个类的对象,可以使用new关键字,并传入构造函数需要的参数。 ```javascript const person = new Person('Tom', 18); console.log(person.name); // 'Tom' console.log(person.age); // 18 person.sayHello(); // 'Hello, my name is Tom.' ``` 上面的代码创建了一个名为person的对象,它的名字是Tom,年龄是18,然后调用了它的sayHello方法输出了自己的名字。 3. 继承 要继承一个类,可以使用extends关键字,并指定父类的名称。 ```javascript class Student extends Person { constructor(name, age, grade) { super(name, age); this.grade = grade; } study() { console.log(`I'm studying in grade ${this.grade}.`); } } ``` 上面的代码定义了一个名为Student的类,它继承自Person类。Student类有一个构造函数和一个方法study。构造函数接受三个参数name、age和grade,其中前两个用于初始化父类的属性,后一个用于初始化自己的属性。study方法用于输出学生的年级。 4. 调用父类方法 在子类中,可以使用super关键字调用父类的方法或属性。 ```javascript class Student extends Person { constructor(name, age, grade) { super(name, age); this.grade = grade; } sayHello() { super.sayHello(); console.log(`I'm a student in grade ${this.grade}.`); } } ``` 上面的代码重新定义了Student类的sayHello方法,并在其中调用了父类的sayHello方法。这样,每次调用子类的sayHello方法时,都会先输出父类的信息,然后再输出子类的信息。 5. 静态方法 类中可以定义静态方法,通过类名调用,而不是通过对象调用。 ```javascript class Calculator { static add(a, b) { return a + b; } static subtract(a, b) { return a - b; } } ``` 上面的代码定义了一个名为Calculator的类,它有两个静态方法add和subtract,用于执行加法和减法运算。这些方法可以直接通过类名调用,而不需要先创建对象。 ```javascript console.log(Calculator.add(1, 2)); // 3 console.log(Calculator.subtract(3, 2)); // 1 ``` 上面的代码分别调用了Calculator类的add和subtract方法,输出了它们的计算结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值