Es6 -- object-oriented

Source: Internet
Author: User
Class

Es5 generates instance objects through Constructors

function User(name, pass) {    this.name = name    this.pass = pass}User.prototype.log = function () {    console.log(‘name = ‘ + this.name + ‘, pass = ‘ + this.pass)}var user1 = new User(‘xiaoming‘, ‘12356‘)user1.log()//name = xiaoming, pass = 12356

Es6 introduces the concept of class and constructor. When defining a "class" method, you do not need to add the function keyword before. Methods do not need to be separated by commas. If they are added, an error is returned.

class User {    constructor(name, pass) {        this.name = name        this.pass = pass    }    log() {        console.log(`name = ${this.name} , pass = ${this.pass}`)    }}let user1 = new User(‘xiaoming‘, ‘12356‘)user1.log()//name = xiaoming , pass = 12356
Inheritance

Es5 inheritance

function VipUser(name, pass, level) {    User.call(this, name, pass)    this.level = level}VipUser.prototype = new User();VipUser.prototype.constructor = VipUserVipUser.prototype.logLevel = function () {    console.log(‘level = ‘ + this.level)}var v1 = new VipUser(‘xiaoming‘, ‘123456‘, ‘5‘)v1.log()v1.logLevel()

Es6 inheritance (extends and super)

class VipUser extends User {    constructor(name, pass, level) {        super(name, pass)        this.level = level    }    logLevel() {        console.log(`level = ${this.level}`)    }}let v1 = new VipUser(‘xiaoming‘, ‘1234‘, ‘5‘)

 

Es6 -- object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.