Creating javascript objects from different files

后端 未结 9 2861
眼角桃花
眼角桃花 2021-02-20 11:15

I\'ve been trying to do javascript for sometime now, but i want it to be \"object-orientated\" so I\'m trying to create different javascript classes in different files and try t

9条回答
  •  生来不讨喜
    2021-02-20 12:00

    Currently JavaScript is not clever enough to find your dependencies without help.

    You need:

    
    
    
        javascript test
        
        
        
    
    
        


    Note:

    If you want on-demand load of the dependencies then you can use AMD (Asynchronous Module Definition) with requirejs or something else.

    Using AMD you can define something like:

    define(['Job', 'person'], function (job, person) {
        //Define the module value by returning a value.
        return function () {};
    });
    

    The define method accepts two arguments: dependencies and function which defines the module. When all dependencies are loaded they are passed as arguments to the function where is the module definition.

    One more thing to notice is that Person and Job are not classes. They are just functions (constructor functions) which produces objects based on rules in their definitions.


提交回复
热议问题