Điều này trong lớp javascript

Lớp để tạo ra các đối tượng [thực chất là một cách thức khác để tạo ra các đối tượng mà Javascript đã biết]. Đối tượng trong nó có các thuộc tính, phương thức. Tạo đối tượng bằng cách sử dụng từ khóa class và khởi tạo hàm constructor

//Khai báo một lớp có tên Product
class Product {
    //Hàm khởi tạo
    constructor[name, price] {
        this.name = name;
        this.price = price;
        this.infomation = `${name} - ${price}`;
    }

    //Khai báo một phương thức
    checkStore[storeid] {
        console.log[this.name + ' in store ' + storeid];
    }

    //Hàm getter
    get info[] {
        return this.infomation;
    }

    //Hàm setter
    set info[i] {
        this.infomation = i;
    }

    //Phương thức tĩnh
    static convertMoney[m] {
        console.log[m];
        return  m + ' đồng';
    }
}

//SỬ DỤNG LỚP

//Tạo một đối tượng từ lớp bằng new
let sanpham = new Product['Iphone', 1000];

//truy cập thuộc tính đối tượng sanpham.name
console.log[sanpham.name];

//gọi một phương thức của đối tượng
sanpham.checkStore[100];

//Gọi setter
sanpham.info = 'Thông tin sản phẩm ...';

//Gọi getter
console.log[sanpham.info];

//Gọi một hàm tĩnh
Product.convertMoney[100000];

Từ đoạn mã trên lưu ý vài vấn đề

  • Lớp khai báo với từ khóa class, bắt buộc có hàm khởi tạo constructor, hàm này được gọi khi khởi tạo đối tượng lớp
    class Computer extends Product {
        constructor[name, price, store] {
            super[name, price];
            this.store = store;
        }
    
        set info[i] {
            //super.info[i] - nếu muốn thi hành phương thức của lớp cha
            this.infomation = name + ':'+i;
        }
    
        totalInStore[] {
            console.log['totalInStore'];
        }
    }
    
    
    //Sử dụng
    let sanpham = new Computer['Dell', 2000];
    console.log[sanpham.name];
    sanpham.checkStore[200];
    
    sanpham.info = 'Thông tin sản phẩm ...';
    
    console.log[sanpham.info];
    sanpham.totalInStore[];
    
    1
  • Các phương thức [checkStore] khai báo trong lớp giống như khai báo hàm nhưng bỏ đi khóa
    class Computer extends Product {
        constructor[name, price, store] {
            super[name, price];
            this.store = store;
        }
    
        set info[i] {
            //super.info[i] - nếu muốn thi hành phương thức của lớp cha
            this.infomation = name + ':'+i;
        }
    
        totalInStore[] {
            console.log['totalInStore'];
        }
    }
    
    
    //Sử dụng
    let sanpham = new Computer['Dell', 2000];
    console.log[sanpham.name];
    sanpham.checkStore[200];
    
    sanpham.info = 'Thông tin sản phẩm ...';
    
    console.log[sanpham.info];
    sanpham.totalInStore[];
    
    2
  • Ở phía trước phương thức có từ khóa
    class Computer extends Product {
        constructor[name, price, store] {
            super[name, price];
            this.store = store;
        }
    
        set info[i] {
            //super.info[i] - nếu muốn thi hành phương thức của lớp cha
            this.infomation = name + ':'+i;
        }
    
        totalInStore[] {
            console.log['totalInStore'];
        }
    }
    
    
    //Sử dụng
    let sanpham = new Computer['Dell', 2000];
    console.log[sanpham.name];
    sanpham.checkStore[200];
    
    sanpham.info = 'Thông tin sản phẩm ...';
    
    console.log[sanpham.info];
    sanpham.totalInStore[];
    
    3, nó được gọi là phương thức getter, nó được gọi là cách truy cập thông tin thuộc tính
  • Ở phía trước phương thức có từ khóa
    class Computer extends Product {
        constructor[name, price, store] {
            super[name, price];
            this.store = store;
        }
    
        set info[i] {
            //super.info[i] - nếu muốn thi hành phương thức của lớp cha
            this.infomation = name + ':'+i;
        }
    
        totalInStore[] {
            console.log['totalInStore'];
        }
    }
    
    
    //Sử dụng
    let sanpham = new Computer['Dell', 2000];
    console.log[sanpham.name];
    sanpham.checkStore[200];
    
    sanpham.info = 'Thông tin sản phẩm ...';
    
    console.log[sanpham.info];
    sanpham.totalInStore[];
    
    4 thì nó là setter, nó được gọi khi được gán [=]
  • Phương thức tĩnh có từ khóa
    class Computer extends Product {
        constructor[name, price, store] {
            super[name, price];
            this.store = store;
        }
    
        set info[i] {
            //super.info[i] - nếu muốn thi hành phương thức của lớp cha
            this.infomation = name + ':'+i;
        }
    
        totalInStore[] {
            console.log['totalInStore'];
        }
    }
    
    
    //Sử dụng
    let sanpham = new Computer['Dell', 2000];
    console.log[sanpham.name];
    sanpham.checkStore[200];
    
    sanpham.info = 'Thông tin sản phẩm ...';
    
    console.log[sanpham.info];
    sanpham.totalInStore[];
    
    0, được gọi qua tên lớp [không thể tham khảo đối tượng lớp qua
    class Computer extends Product {
        constructor[name, price, store] {
            super[name, price];
            this.store = store;
        }
    
        set info[i] {
            //super.info[i] - nếu muốn thi hành phương thức của lớp cha
            this.infomation = name + ':'+i;
        }
    
        totalInStore[] {
            console.log['totalInStore'];
        }
    }
    
    
    //Sử dụng
    let sanpham = new Computer['Dell', 2000];
    console.log[sanpham.name];
    sanpham.checkStore[200];
    
    sanpham.info = 'Thông tin sản phẩm ...';
    
    console.log[sanpham.info];
    sanpham.totalInStore[];
    
    1]

Tính kế thừa của lớp

Giống như nhiều ngôn ngữ lập trình hướng đối tượng, JS cung cấp khả năng tạo ra một lớp mới có kế thừa thuộc tính, phương thức của một lớp khác. To create a class con next from the layer cha used from

class Computer extends Product {
    constructor[name, price, store] {
        super[name, price];
        this.store = store;
    }

    set info[i] {
        //super.info[i] - nếu muốn thi hành phương thức của lớp cha
        this.infomation = name + ':'+i;
    }

    totalInStore[] {
        console.log['totalInStore'];
    }
}


//Sử dụng
let sanpham = new Computer['Dell', 2000];
console.log[sanpham.name];
sanpham.checkStore[200];

sanpham.info = 'Thông tin sản phẩm ...';

console.log[sanpham.info];
sanpham.totalInStore[];
2

class Computer extends Product {
    constructor[name, price, store] {
        super[name, price];
        this.store = store;
    }

    set info[i] {
        //super.info[i] - nếu muốn thi hành phương thức của lớp cha
        this.infomation = name + ':'+i;
    }

    totalInStore[] {
        console.log['totalInStore'];
    }
}


//Sử dụng
let sanpham = new Computer['Dell', 2000];
console.log[sanpham.name];
sanpham.checkStore[200];

sanpham.info = 'Thông tin sản phẩm ...';

console.log[sanpham.info];
sanpham.totalInStore[];

Ở ví dụ trên bạn đã tạo ra một lớp mới có tên

class Computer extends Product {
    constructor[name, price, store] {
        super[name, price];
        this.store = store;
    }

    set info[i] {
        //super.info[i] - nếu muốn thi hành phương thức của lớp cha
        this.infomation = name + ':'+i;
    }

    totalInStore[] {
        console.log['totalInStore'];
    }
}


//Sử dụng
let sanpham = new Computer['Dell', 2000];
console.log[sanpham.name];
sanpham.checkStore[200];

sanpham.info = 'Thông tin sản phẩm ...';

console.log[sanpham.info];
sanpham.totalInStore[];
3 kế thừa lớp cha là
class Computer extends Product {
    constructor[name, price, store] {
        super[name, price];
        this.store = store;
    }

    set info[i] {
        //super.info[i] - nếu muốn thi hành phương thức của lớp cha
        this.infomation = name + ':'+i;
    }

    totalInStore[] {
        console.log['totalInStore'];
    }
}


//Sử dụng
let sanpham = new Computer['Dell', 2000];
console.log[sanpham.name];
sanpham.checkStore[200];

sanpham.info = 'Thông tin sản phẩm ...';

console.log[sanpham.info];
sanpham.totalInStore[];
4, do đó lóp con có các thuộc tính, phương thức như Lớp cha và khai báo thêm các thuộc tính của phương thức riêng

Chủ Đề