Last active
August 29, 2015 14:02
-
-
Save ww24/951b7731ad81c6ca53b4 to your computer and use it in GitHub Desktop.
Global 名前空間のどの変数名をライブラリに使わせるか、利用者が選べるようにする仕組み。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://fiddle.jshell.net/ww24/EyNfe/2/