Composition vs Inheritance in JavaScript

Video

JavaScript Notes

JavaScript
    // inherit-compose.js
    // Inheritance vs Composition
    
    // INHERITANCE
    // Character > Human > Sam
    // Character > Robot > x73
    // Character > Cyborg > Dolph
    
    const Character = {
        talk: function(...msg){
            console.log(msg.join(' '))
        }
    }
    
    const Human = Object.create( Character, { 
        speed: {value: 3}, 
        name: {value: 'Hector'}
    });
    
    Human.walk = function( ){
        this.talk(this.name, 'walking')
    };
    
    Human.eat = function( ){
        this.talk(this.name, 'eating')
    };
    
    const Robot = Object.create(Character, { 
        speed: {value: 8}, 
        id: {value: 'THX1138'}
    });
    
    Robot.drive = function( ){
        this.talk(this.id, '\u26A1', 'driving')
    };
    
    Robot.wifi = function( ){
        this.talk(this.id, '\u26A1', 'connecting')
    };
    
    const sam = Object.create(Human, {
        name:{value: 'Samuel'}
    });
    
    sam.walk ( );
    sam.talk ('Hello from Samuel');
    
    const x73 = Object.create(Robot, {
        id:{value: 'x73'}
    });

    x73.drive( ) x73.wifi( )
    

    // ??What about a Cybernetically enhanced human with WiFi?
    //COMPOSITION
    const talker = (state) =>({
        talk: (msg) => console.log(msg.join(' '))
    });
    const walker = (state) =>({
        walk: ( ) => {
            let nm = state.name || state.id;
            console.log(nm, 'walking');
        }
    });
    const eater = (state) =>({
        eat: ( ) => {
            let nm = state.name || state.id;
            console.log(nm, 'eating');
    }
    });
    const driver = (state) =>({
        drive: ( ) => {
            let nm = state.name || state.id; 
            console.log(nm, \'u26A1', 'driving');
        }
    });
    const wifier = (state) =>({
        wifi: ( ) => {
            let nm = state.name || state.id; 
            console.log(nm, \'u26A1', 'connecting');
        }
    });
    
    const Person = (name, speed = 3) => {	
        // speed is default of 3 if not set 
        let state = {
            name, speed
        }
     
        return Object.assign( { },
        talker(state), walker(state), eater(state) );
    };
    
    let Bob = Person('Bob');
    Bob.talk('Hello from Bob');
    Bob.eat( );
    
    const Android = (id, speed = 6) => {	
        // speed is default of 6 if not set 
        let state = {
            id, speed
        }
        return Object.assign( { },
        talker(state), driver(state), wifier(state));
    };

    let k45 = Android('k45');
    k45.drive( );
    k45.wifi( );
    
    const Cybord = (id, speed) => {	
        // speed is default of 6 if not set 
        let state = {
            id, speed
        }
        return Object.assign( { },
        talker(state), walker(state), wifier(state));
    };

    let Dolph = Cybord('Dolph', 9);
    Dolph.walk( );
    Dolph.wifi( );