Skip to content

Instantly share code, notes, and snippets.

@ww24
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ww24/951b7731ad81c6ca53b4 to your computer and use it in GitHub Desktop.
Save ww24/951b7731ad81c6ca53b4 to your computer and use it in GitHub Desktop.
Global 名前空間のどの変数名をライブラリに使わせるか、利用者が選べるようにする仕組み。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>mylib</title>
</head>
<body>
<script src="mylib.js" data-mylib="lib"></script>
<script>
addEventListener("load", function () {
lib.hoge = 1;
lib.piyo();
});
</script>
</body>
</html>
addEventListener("load", function () {
var globalName = [].filter.call(document.scripts, function (script) {
return script.dataset.mylib;
})[0].dataset.mylib || "mylib";
myNameSpace = {
hoge: 0,
piyo: function () {
console.log(this.hoge);
}
};
window[globalName] = myNameSpace;
console.log(globalName);
});
@ww24
Copy link
Author

ww24 commented Jun 6, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment