看了赵劼老师的深入浅出ajax了解了点javascript仿面向对象的编程
初次接触,根据视频中的讲解,记录下第一部分的基本代码:
先记录这些~~
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
- <!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 runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <!--这个控件是必须的-->
- <asp:ScriptManager runat="server" ID="scriptManager1">
- </asp:ScriptManager>
- <script language="javascript" type="text/javascript">
- //用type类型申明一个命名空间 AjaxNameSpaceTest
- Type.registerNamespace("AjaxNameSpace");
- // 相当于给上面命名空间中添加一个Person类的构造函数
- AjaxNameSpace.Person=function(firstName,lastName){
- //js类型中以 _开头的变量为私有变量
- this._firstName=firstName;
- this._lastName=lastName;
- }
- //添加get set属性,和方法
- AjaxNameSpace.Person.prototype={
- get_firstName:function(){
- return this._firstName;
- },
- set_firstName:function(){
- this._firstName=value;
- },
- get_lastName:function(){
- return this._lastName;
- },
- //toString()方法 这个应该不陌生
- toString:function(){
- return String.format("Hi,I'm {0} {1}.",this.get_firstName(),this.get_lastName());
- }
- }
- //注册类
- AjaxNameSpace.Person.registerClass("AjaxNameSpace.Person");
- //再注册一个类 继承上面的AjaxNamespace.Person
- AjaxNameSpace.Employee=function(firstName,lastName,title){
- //调用基类的构造函数
- AjaxNameSpace.Employee.initializeBase(this,[firstName,lastName]);
- this._title=title;
- }
- AjaxNameSpace.Employee.prototype={
- get_title:function(){
- return this._titel;
- }
- //重写方法toString
- toString:function(){
- //callBaseMethod()方法 调用基类方法
- return AjaxNameSpace.Employee.callBaseMethod(this,"toString") + "My title is '"+this._title +"'.";
- }
- }
- //注册类并说明继承自那个类
- AjaxNameSpace.Employee.registerClass("AjaxNamespace.Employee",AjaxNameSpace.Person);
- </script>
- <input type="button" value="Lu Guang" onclick="alert(new AjaxNameSpace.Person('Lu','Guang'));" />
- <input type="button" value="Huang Yan" onclick="alert(new AjaxNameSpace.Employee('Huang','Yan','Student'));" />
- </form>
- </body>
- </html>