Constructors in JavaScript objects

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

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

19条回答
  •  [愿得一人]
    2020-11-22 10:51

    just to offer up some variety. ds.oop is a nice way to declare classes with constructors in javascript. It supports every possible type of inheritance (Including 1 type that even c# does not support) as well as Interfaces which is nice.

    var Color = ds.make.class({
        type: 'Color',
        constructor: function (r,g,b) { 
            this.r = r;                     /* now r,g, and b are available to   */
            this.g = g;                     /* other methods in the Color class  */
            this.b = b;                     
        }
    });
    var red = new Color(255,0,0);   // using the new keyword to instantiate the class
    

提交回复
热议问题