⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.htm

📁 javascript book
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>创建新对象</title>
<meta name="Microsoft Theme" content="mstheme5474 011">
</head>

<body background="maizbk.jpg" bgcolor="#FFFFFF" text="#000000" link="#800000" vlink="#000080" alink="#3366CC">

<!--mstheme--><font face="宋体"><!--mstheme--></font>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%"><!--mstheme--><font face="宋体">
      <p align="center"><b><font class="title" lang="ZH-CN" size="4" color="#000000">创建新对象</font></b></p>
      <p style="line-height: 150%"><font face="宋体"><b>  </b>使<font size="3">用JavaScript可以创建自己的对象。虽然JavaScript内部和浏览器本身的功能已十分强大,但JavaScript还是提供了创建一个新对象的方法。使其不必像超文本标识语言那样,求于或其它多媒体工具,就能完成许多复杂的工作。<br>
      在JavaScript中创建一个新的对象是十分简单的。首先它必须定义一个对象,而后再为该对象创建一个实例。这个实例就是一个新对象,它具有对象定义中的基本特征。</font></font></p>
      <p style="line-height: 150%"><b><font face="宋体" size="3">一、对象的定义</font></b></p>
      <p style="line-height: 150%"><font face="宋体" size="3">JavaScript对象的定义,其基本格式如下:</font></p>
      <p style="line-height: 150%"><font face="宋体" size="3">Function Object(属性表)</font></p>  
      <!--mstheme--></font> 
      <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
        <!--msthemelist--><tr>
          <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu1.gif" width="15" height="15" hspace="13"></td>
          <td valign="top" width="100%"><!--mstheme--><font face="宋体"> 
          <p style="line-height: 150%">  
            <!--mstheme--></font>
            <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体"> 
              <p style="line-height: 150%"><font face="宋体" size="3">This.prop1=prop1</font> 
              <p style="line-height: 150%"><font face="宋体" size="3">This.prop2=prop2</font></p> 
              <p style="line-height: 150%"><font face="宋体" size="3">...</font></p> 
              <p style="line-height: 150%"><font face="宋体" size="3">This.meth=FunctionName1;</font></p> 
              <p style="line-height: 150%"><font face="宋体" size="3">This.meth=FunctionName2;</font></p> 
              <p style="line-height: 150%"><font face="宋体" size="3">...</font></p> 
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
            <!--msthemelist--></table>
            <!--mstheme--><font face="宋体"><!--mstheme--></font><!--msthemelist--></td>
        </tr>
      <!--msthemelist--></table>
      <!--mstheme--><font face="宋体"> 
      <p style="line-height: 150%"><font face="宋体" size="3">在一个对象的定义中,可以为该对象指明其属性和方法。通过属性和方法构成了一个对象的实例。如以下是一个关于University对象的定义:</font></p> 
      <p style="line-height: 150%"><font face="宋体" size="3">Function   
      university(name,city,creatDate URL)</font></p>  
      <!--mstheme--></font> 
      <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
        <!--msthemelist--><tr>
          <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu1.gif" width="15" height="15" hspace="13"></td>
          <td valign="top" width="100%"><!--mstheme--><font face="宋体"> 
          <p style="line-height: 150%"><font size="3"> </font>
            <!--mstheme--></font>
            <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3">This.name=name</font>
              <p style="line-height: 150%"><font face="宋体" size="3">This.city=city</font></p>
              <p style="line-height: 150%"><font face="宋体" size="3">This.creatDate=New 
              Date(creatDate)</font></p>
              <p style="line-height: 150%"><font face="宋体" size="3">This.URL=URL</font></p>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
            <!--msthemelist--></table>
            <!--mstheme--><font face="宋体"><!--mstheme--></font><!--msthemelist--></td>
        </tr>
      <!--msthemelist--></table>
      <!--mstheme--><font face="宋体">
      <p style="line-height: 150%"><font face="宋体" size="3">其基本含义如下:</font></p>
      <!--mstheme--></font>
      <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
        <!--msthemelist--><tr>
          <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu1.gif" width="15" height="15" hspace="13"></td>
          <td valign="top" width="100%"><!--mstheme--><font face="宋体">
          <p style="line-height: 150%"> 
            <!--mstheme--></font>
            <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3"> </font>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3">Name-指定一个“单位”名称。</font>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3"> </font>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3">City-“单位”所在城市。</font>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3"> </font>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3">CreatDate-记载university对象的更新日期。</font>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3"> </font>
                  <!--mstheme--></font><!--msthemelist--></td>
              </tr>
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3">URL-该对象指向一个网址。</font><!--mstheme--></font><!--msthemelist--></td>
              </tr>
            <!--msthemelist--></table>
            <!--mstheme--><font face="宋体"><!--mstheme--></font><!--msthemelist--></td>
        </tr>
      <!--msthemelist--></table>
      <!--mstheme--><font face="宋体">
      <p style="line-height: 150%"><font face="宋体" size="3"> </font></p>
      <p style="line-height: 150%"><b><font face="宋体" size="3">二、创建对象实例</font></b></p>
      <p style="line-height: 150%"><font face="宋体" size="3">一旦对象定义完成后,就可以为该对象创建一个实例了:</font></p>
      <!--mstheme--></font>
      <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
        <!--msthemelist--><tr>
          <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu1.gif" width="15" height="15" hspace="13"></td>
          <td valign="top" width="100%"><!--mstheme--><font face="宋体">
          <p style="line-height: 150%"> 
            <!--mstheme--></font>
            <!--msthemelist--><table border="0" cellpadding="0" cellspacing="0" width="100%">
              <!--msthemelist--><tr>
                <!--msthemelist--><td valign="baseline" width="42"><img src="amaizbu2.gif" width="12" height="12" hspace="15"></td>
                <td valign="top" width="100%"><!--mstheme--><font face="宋体">
              <p style="line-height: 150%"><font face="宋体" size="3">NewObject=New   
              object();</font><!--mstheme--></font><!--msthemelist--></td> 
              </tr> 
            <!--msthemelist--></table> 
            <!--mstheme--><font face="宋体"><!--mstheme--></font><!--msthemelist--></td>
        </tr>
      <!--msthemelist--></table>
      <!--mstheme--><font face="宋体"> 
      <p style="line-height: 150%"><font face="宋体" size="3">其中Newobjet是新的对象,Object已经定义好的对象。例:</font></p> 
      <p style="line-height: 150%"><font face="宋体" size="3">U1=New university(“云南省”,“昆明市”,&quot;January   
      05,199712:00:00&quot;,&quot;http://www.YN.KM&quot;)</font></p>  
      <p style="line-height: 150%"><font face="宋体" size="3">U2=New university(“云南电子科技大学”,“昆明”,&quot;January   
      07,1997 12:00:00&quot;,&quot;htlp://www.YNKJ.CN&quot;)</font></p>  
      <p style="line-height: 150%"><font face="宋体" size="3"> </font></p>
      <p style="line-height: 150%"><b><font face="宋体" size="3">三、对象方法的使用</font></b></p>
      <p style="line-height: 150%"><font face="宋体" size="3">在对象中除了使用属性外,有时还需要使用方法。在对象的定义中,我们看到This.meth=FunctionName语句,那就是为定义对象的方法。实质对象的方法就是一个函数FunctionName,通过它实现自己的意图。</font></p>
      <p style="line-height: 150%"><font face="宋体" size="3">例在university对象中增加一个方法,该方法是显示它自己本身,并返回相应的字串。</font></p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -