JavaScriptは名前空間、クラス及びクラスメンバーを作成します.
8579 ワード
JavaScriptコード:
HTMLコード:
// String isNullOrEmpty
String.isNullOrEmpty = function (a)
{
if (a != null && a != "" && a != "undefined")
return false;
return true;
}
// window/function usingNamespace
window.usingNamespace = Function.usingNamespace = function (a) {
var ro = window;
if (String.isNullOrEmpty(a)) {
return ro;
};
var co = ro;
var nsp = a.split(".");
for (var i = 0; i < nsp.length; i++) {
var cp = nsp[i];
if (!ro[cp]) {
ro[cp] = {};
};
co = ro = ro[cp];
};
return co;
};
// "sss.bbb"
// aaa
// msg,name,school,todo()
usingNamespace("sss.bbb")["aaa"] = {
msg: " ",
name: " ",
school: " ",
todo: function ()
{
alert(this.name+","+this.msg+"! "+this.school+" ! ");
}
}
HTMLコード:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="myJS.js"></script>
</head>
<body>
<form name="MyForm">
<input type="button" name="MyButton" value="Click me!" onclick="alert(sss.bbb.aaa.msg);" /><br />
<input type="button" name="MyButton" value="Click me!" onclick="alert(sss.bbb.aaa.name);" /><br />
<input type="button" name="MyButton" value="Click me!" onclick="sss.bbb.aaa.todo()" /><br />
</form>
</body>
</html>