3 added 19 characters in body
source | link

If your writing an enterprise application and want to keep your memory usage under 1GB avoid unnecessarily using closures like the plagueto store state.

If your writing an enterprise application and want to keep your memory usage under 1GB avoid closures like the plague.

If your writing an enterprise application and want to keep your memory usage under 1GB avoid unnecessarily using closures to store state.

2 added 771 characters in body
source | link

Live Example

(function _anonymouswrapper(undefined) { 

    var Skillet = {
        constructor: function (options) {
            options && extend(this, options);
            return this; 
        },
        ingredient: "Bacon Strips",
        _isHot: true,
        fry: function fry(oliveOil) {
            this._addItem("\t\n Butter \n\t");
            this._addItem(oliveOil);
            this._addItem(this.ingredient);
            console.log("Frying " + this.ingredient);
        },
        addItem_addItem: function addItem(item) {
            console.log("Adding " + item.toString().trim());
        }
    };

    consolevar skillet = Object.logcreate(Skillet).ingredientconstructor(); 

    console.log(Skilletskillet.ingredient);
    skillet.fry()"olive oil"); 

    Skillet.quantityvar PrintableSkillet = "12";extend(Object.create(Skillet), {
    console.log    constructor: function constructor(Skillet.quantityoptions) {
            options && extend(this, options);
            return this;
    Skillet._amountOfGrease = "1 Cup"; },
    Skillet.toString = toString;  _amountOfGrease: "1 Cup",
        quantity: 12,
        toString: function toString() {
            console.log(this.quantity + " " +
                        this.ingredient + " & " +
                        this._amountOfGrease + " of Grease");
            console.log(this._isHot ? "Hot" : "Cold");
        }
    });

    Skilletvar skillet = Object.create(PrintableSkillet).constructor();

    skillet.toString();

    function extend(target, source) {
        Object.getOwnPropertyNames(source).forEach(function (name) {
            var pd = Object.getOwnPropertyDescriptor(source, name);
            Object.defineProperty(target, name, pd);
        });
        return target;
    }
}());
(function _anonymouswrapper(undefined) {
    var Skillet = {
        ingredient: "Bacon Strips",
        _isHot: true,
        fry: function fry(oliveOil) {
            this._addItem("\t\n Butter \n\t");
            this._addItem(oliveOil);
            this._addItem(this.ingredient);
            console.log("Frying " + this.ingredient);
        },
        addItem: function addItem(item) {
            console.log("Adding " + item.toString().trim());
        }
    };

    console.log(Skillet.ingredient);
    console.log(Skillet.fry());
    Skillet.quantity = "12";
    console.log(Skillet.quantity);

    Skillet._amountOfGrease = "1 Cup";
    Skillet.toString = toString;

    function toString() {
        console.log(this.quantity + " " +
                    this.ingredient + " & " +
                    this._amountOfGrease + " of Grease");
        console.log(this._isHot ? "Hot" : "Cold");
    }

    Skillet.toString();
}());

Live Example

(function _anonymouswrapper(undefined) { 

    var Skillet = {
        constructor: function (options) {
            options && extend(this, options);
            return this; 
        },
        ingredient: "Bacon Strips",
        _isHot: true,
        fry: function fry(oliveOil) {
            this._addItem("\t\n Butter \n\t");
            this._addItem(oliveOil);
            this._addItem(this.ingredient);
            console.log("Frying " + this.ingredient);
        },
        _addItem: function addItem(item) {
            console.log("Adding " + item.toString().trim());
        }
    };

    var skillet = Object.create(Skillet).constructor(); 

    console.log(skillet.ingredient);
    skillet.fry("olive oil"); 

    var PrintableSkillet = extend(Object.create(Skillet), {
        constructor: function constructor(options) {
            options && extend(this, options);
            return this;
        },
        _amountOfGrease: "1 Cup",
        quantity: 12,
        toString: function toString() {
            console.log(this.quantity + " " +
                        this.ingredient + " & " +
                        this._amountOfGrease + " of Grease");
            console.log(this._isHot ? "Hot" : "Cold");
        }
    });

    var skillet = Object.create(PrintableSkillet).constructor();

    skillet.toString();

    function extend(target, source) {
        Object.getOwnPropertyNames(source).forEach(function (name) {
            var pd = Object.getOwnPropertyDescriptor(source, name);
            Object.defineProperty(target, name, pd);
        });
        return target;
    }
}());
1
source | link

(function _anonymouswrapper(undefined) {
    var Skillet = {
        ingredient: "Bacon Strips",
        _isHot: true,
        fry: function fry(oliveOil) {
            this._addItem("\t\n Butter \n\t");
            this._addItem(oliveOil);
            this._addItem(this.ingredient);
            console.log("Frying " + this.ingredient);
        },
        addItem: function addItem(item) {
            console.log("Adding " + item.toString().trim());
        }
    };

    console.log(Skillet.ingredient);
    console.log(Skillet.fry());
    Skillet.quantity = "12";
    console.log(Skillet.quantity);

    Skillet._amountOfGrease = "1 Cup";
    Skillet.toString = toString;

    function toString() {
        console.log(this.quantity + " " +
                    this.ingredient + " & " +
                    this._amountOfGrease + " of Grease");
        console.log(this._isHot ? "Hot" : "Cold");
    }

    Skillet.toString();
}());

You can use a IIFE to emulate "module scope" around your code. Then you can just use objects as you normally do.

Don't "emulate" private state using closures as that has a large memory penalty.

If your writing an enterprise application and want to keep your memory usage under 1GB avoid closures like the plague.