Storing a variable in the JavaScript 'window' object is a proper way to use that object?

前端 未结 3 1669
日久生厌
日久生厌 2020-11-28 05:56

(Maybe) I just solved a my problem (How to update front-end content after that a form is successfully submitted from a dialog window?) by \"storing\" / \"saving\" a variable

3条回答
  •  误落风尘
    2020-11-28 06:32

    Another pattern to make a unique namespace in your application.

    function myApp()
    {
        var value = 5;
        function addToValue(x)
        {
             value += x;
        }
    }()
    

    If you want functions/values to be accessible afterwards you can store them in an object like this:

    function myApp()
    {
        this.value = 5;
        this.addToValue = function(x)
        {
             this.value += x;
        }
    }
    var myInstance = new myApp();
    

提交回复
热议问题