What techniques can be used to define a class in JavaScript, and what are their trade-offs?

后端 未结 19 1479
庸人自扰
庸人自扰 2020-11-22 07:26

I prefer to use OOP in large scale projects like the one I\'m working on right now. I need to create several classes in JavaScript but, if I\'m not mistaken, there are at le

19条回答
  •  心在旅途
    2020-11-22 07:48

    You probably want to create a type by using the Folding Pattern:

        // Here is the constructor section.
        var myType = function () {
            var N = {}, // Enclosed (private) members are here.
                X = this; // Exposed (public) members are here.
    
            (function ENCLOSED_FIELDS() {
                N.toggle = false;
                N.text = '';
            }());
    
            (function EXPOSED_FIELDS() {
                X.count = 0;
                X.numbers = [1, 2, 3];
            }());
    
            // The properties below have access to the enclosed fields.
            // Careful with functions exposed within the closure of the
            // constructor, each new instance will have it's own copy.
            (function EXPOSED_PROPERTIES_WITHIN_CONSTRUCTOR() {
                Object.defineProperty(X, 'toggle', {
                    get: function () {
                        var before = N.toggle;
                        N.toggle = !N.toggle;
                        return before;
                    }
                });
    
                Object.defineProperty(X, 'text', {
                    get: function () {
                        return N.text;
                    },
                    set: function (value) {
                        N.text = value;
                    }
                });
            }());
        };
    
        // Here is the prototype section.
        (function PROTOTYPE() {
            var P = myType.prototype;
    
            (function EXPOSED_PROPERTIES_WITHIN_PROTOTYPE() {
                Object.defineProperty(P, 'numberLength', {
                    get: function () {
                        return this.numbers.length;
                    }
                });
            }());
    
            (function EXPOSED_METHODS() {
                P.incrementNumbersByCount = function () {
                    var i;
                    for (i = 0; i < this.numbers.length; i++) {
                        this.numbers[i] += this.count;
                    }
                };
                P.tweak = function () {
                    if (this.toggle) {
                        this.count++;
                    }
                    this.text = 'tweaked';
                };
            }());
        }());
    

    That code will give you a type called myType. It will have internal private fields called toggle and text. It will also have these exposed members: the fields count and numbers; the properties toggle, text and numberLength; the methods incrementNumbersByCount and tweak.

    The Folding Pattern is fully detailed here: Javascript Folding Pattern

提交回复
热议问题