1
1

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 5 years have passed since last update.

Javascriptで階層を持った名前空間を定義する関数

1
Posted at
function namespace(ns) {
    var names = ns.split('.');
    var parent = window;

    for (var i = 0, len = names.length; i < len; i++) {
        parent[names[i]] = parent[names[i]] || {};
        parent = parent[names[i]];
        console.info(parent);
    }
    return parent;
}

// 実行確認
var aaa = namespace('Hoge.Fuga');
aaa.Piyo = function () {};
var bbb = new aaa.Piyo;
console.info(bbb instanceof Hoge.Fuga.Piyo);

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?