Constructors in JavaScript objects

后端 未结 19 1749
夕颜
夕颜 2020-11-22 10:21

Can JavaScript classes/objects have constructors? How are they created?

19条回答
  •  长发绾君心
    2020-11-22 10:33

    Example here: http://jsfiddle.net/FZ5nC/

    Try this template:

    
    

    You must adjust your namespace if you are defining a static class:

    
    

    Example class:

    
    

    Example instantiation:

    
    
    

    Notice functions are defined as A.B = function A_B(). This is to make your script easier to debug. Open Chrome's Inspect Element panel, run this script, and expand the debug backtrace:

    
    

提交回复
热议问题