88
87

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptの名前空間定義チートシート

Last updated at Posted at 2014-02-02

チートシートなので説明なし。

JavaScriptでの名前空間の書き方。

var top = top || {}; // ancestor namespace
top.second = top.second || {}; // ancestor namespace

(function (third) { // namespace
    // private property
    var privPropName = "privPropValue";

    // private method
    function privFuncName() {
    }

    // public function
    third.funcName = function () {
    }

    // public class
    third.ClassName = (function () {
        function ClassName() {
        }
        return ClassName;
    })();

    // public singleton object    
    third.objectName = (function () {
        return {
        };
    })();

})(top.second.third = top.second.third || {});

// usage
top.second.third.funcName();
var instance = new top.second.third.ClassName();
top.second.third.objectName;
88
87
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
88
87

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?