I'd like to know if I understood the line from the code.



  • let allModels = {};
    
    function createModel(Model, ...args) {
        let model = new Model(...args);
    
        model._id = Math.random().toString(36).slice(2);
        allModels[model._id] = model;
    
        return model;
    }
    
    let user = createModel(class User {
        constructor(name) {
            this.name = name;
        }
        sayHi() {
            alert(this.name);
        }
    }, "Вася");
    
    user.sayHi(); // Вася
    
    alert( allModels[user._id].name ); // Вася
    

    allModels[model._id] = model; I'm interested in this line, and I've learned that it's true. model._id from the site model copyed allModels?
    Thank you for your help!



  • That's right. model._id from the site model copyed allModels?

    No, not right.

    In this line

    allModels[model._id] = model;
    

    There are two actions:

    1. Access to an equal value character model._id
    2. The value in question is assigned model

    So the object allModels keep all facilities that have been created by the function Model


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2