js 注册命名空间(registerNamespace )
创建一个命名空间。此成员是静态的,可在不创建类实例的情况下调用。Type.registerNamespace(namespacePath).其中namespacePath一个字符串,表示要注册的完全限定命名空间。
备注:使用 registerNamespace 方法可以创建命名空间。命名空间是使您可以组织代码的范围区域。这对于大型项目特别有用。使用命名空间可以对类进行分组。这样,每个类名便由其命名空间限定,从而可以创建全局唯一类名。
在一个命名空间中,可以声明下列一个或多个类:
-
其他命名空间
-
类
-
接口
-
枚举
-
委托
一个完全限定类名包含其命名空间,并以 .(点)运算符分隔,如 MyNamespace.MyType。命名空间名称可以包含 A–Z 范围内的大写或小写字母以及 0-9 范围内的整数。
下面的示例演示如何使用 registerNamespace 方法注册命名空间 Samples:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Sample</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<script type="text/javascript">
// Register classes to test.
Type.registerNamespace('Samples');
Samples.A = function()
{
// Initialize as a base class.
Samples.A.initializeBase(this);
}
Samples.B = function(){}
Samples.C = function(){}
Samples.A.registerClass('Samples.A');
Samples.B.registerClass('Samples.B', Samples.A);
Samples.C.registerClass('Samples.C');
var isDerived;
isDerived = Samples.B.inheritsFrom(Samples.A);
// Output: "true".
alert(isDerived);
isDerived = Samples.C.inheritsFrom(Samples.A);
// Output: "false".
alert(isDerived);
</script>
</form>
</body>
</html>
浙公网安备 33010602011771号