es6 class of basic grammar, es6 class inheritance / es5 call inherit Description / compare inherited Array perfect way to use apply

// basic grammar 
function
People (name, Email) { name, email } class User{ constructor(name,email){ this.name = name; this.email = email; } getinfo(){ console.log(this.name); } static description(){ console.log('I am description --static'); } set urls(values){ console.log(values); this.urlsvalue = values; } get urls(){ return `hello urls ${this.urlsvalue}`; } }



// class implementation inheritance (included with es5 inherited methods)
 class Animal {
    constructor(name){
        this.name = name;
        this.belly = [];
    }
    eat(food){
        this.belly.push(food); }
    speak(){
        console.log('I am lucky') }
}
class Dog extends Animal{
    constructor(name,age){
        super(name);
        this.age = age;
    }
    bark(){
        console.log('bark bark!'); } speak(){
        console.log('bark bark I am lucky') } } Lucky new new Dog = const ( 'Lucky', 2 ) the console.log (lucky.speak ()) // Bark Bark with a method of the I AM Lucky override inherited methods

// inheritance es5 relatively complex 
// Start 
// function Dog (name, Age) { 
//      Animal.call (the this, name, Age); // constructor subclass which first calls the base class constructor, es6 direct super () can be. 
//      this.name = name; 
//      this.age = Age; 
// } 
// Dog.prototype = new new Animal (); // Dog prototype object is pointed Animal as, constructor will be changed, it is necessary to declare the following under construcor point Dog 
// Dog.prototype.constructor = Dog;
// end
 
// ES5 use apply more perfect way to start inherited Array

function MyArray(){
  Array.apply(this,arguments);
}
MyArray.prototype = new Array()
MyArray.prototype.constructor = MyArray
const colors = new MyArray();

colors[0] = 'red';
console.log(colors.length);

colors.length = 0;
console.log(colors[0]);
// es5 use apply more perfect way to end inheritance Array
 

Guess you like

Origin www.cnblogs.com/jwzhang/p/12104841.html