`
saybody
  • 浏览: 911326 次
  • 性别: Icon_minigender_2
  • 来自: 西安
文章分类
社区版块
存档分类
最新评论

JavaScript,咱也OO一把

 
阅读更多
<div class="floatRPic">
<a title="javascript的面向对象" href="http://justinyoung.cnblogs.com/" target="_blank"><img alt="web标准设计" src="http://images.cnblogs.com/cnblogs_com/justinyoung/2007/oo.jpg"></a><br>
OO不是(.)(.)</div>
<p><font>惭愧,惭愧,JavaScript一直是俺的软肋。特别是对Javascript模拟面向对象编程这块,一看到javascript代码中含有什么prototype,"=function()"这样的代码,就敬如神明,心想定是高手所为。这段时间一直在看《Javascript高级程序设计》这本书,其中有些章节,就专门讲道了这个方面,看后,不敢说了然于胸,但是倒是也能够写个小程序,体验了一把。嘿嘿……JavaScript,今天咱也OO一把。</font></p>
<p>这个例子很简单,也很实用,就是在博客园的博客上方,显示一篇随机推荐的文章。这样,有时候,碰巧,能粘住浏览者一会。呵呵~</p>
<h5>小打小闹,高人回避~</h5>
<h4>先说一下,实现思想</h4>
<h5>1:每篇推荐文章都是一个对象</h5>
<p>这个对象有2个属性(URL和Title),分别是文章的URL地址和文章标题,和一个方法(commendMe),用于将自己添加到top导航栏后面。</p>
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee;">
<span style="color: #0000ff;">function</span><span style="color: #000000;">article(sURL,sTitle){<br></span><span style="color: #0000ff;">this</span><span style="color: #000000;">.URL</span><span style="color: #000000;">=</span><span style="color: #000000;">sURL;<br></span><span style="color: #0000ff;">this</span><span style="color: #000000;">.title</span><span style="color: #000000;">=</span><span style="color: #000000;">sTitle;<br>
}<br>
article.prototype.URL</span><span style="color: #000000;">=</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){<br></span><span style="color: #0000ff;">return</span><span style="color: #000000;">(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.URL);<br>
}<br>
article.prototype.title</span><span style="color: #000000;">=</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){<br></span><span style="color: #0000ff;">return</span><span style="color: #000000;">(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.title);<br>
}<br>
article.prototype.commendMe</span><span style="color: #000000;">=</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){<br></span><span style="color: #0000ff;">var</span><span style="color: #000000;">comArticleURL</span><span style="color: #000000;">=</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.URL;<br></span><span style="color: #0000ff;">var</span><span style="color: #000000;">comArticleTitle</span><span style="color: #000000;">=</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.title;<br></span><span style="color: #0000ff;">var</span><span style="color: #000000;">commendatoryArticleHTML</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">&amp;nbsp;&lt;imgstyle=\</span><span style="color: #000000;">"</span><span style="color: #000000;">position:relative;top:3px;\</span><span style="color: #000000;">"</span><span style="color: #000000;">src=\</span><span style="color: #000000;">"</span><span style="color: #000000;">http:</span><span style="color: #008000;">//</span><span style="color: #008000;">www.cnblogs.com/images/cnblogs_com/justinyoung/2007/broadcast.gif\"alt=\"推荐阅读文章\"/&gt;推荐阅读:"+"&lt;atitle=\""+comArticleTitle+"\"href=\""+comArticleURL+"\"&gt;"+comArticleTitle+"&lt;/a&gt;";;</span><span style="color: #008000;"><br></span><span style="color: #000000;"></span><span style="color: #0000ff;">var</span><span style="color: #000000;">mylinks</span><span style="color: #000000;">=</span><span style="color: #000000;">document.getElementById(</span><span style="color: #000000;">"</span><span style="color: #000000;">mylinks</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #0000ff;">var</span><span style="color: #000000;">commendatoryArticle</span><span style="color: #000000;">=</span><span style="color: #000000;">document.createElement('span');<br>
commendatoryArticle.innerHTML</span><span style="color: #000000;">=</span><span style="color: #000000;">commendatoryArticleHTML;<br>
mylinks.appendChild(commendatoryArticle);<br>
}<br></span>
</div>
<p>2:定义一个数组,数组里面包含很多的文章对象</p>
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee;">
<span style="color: #000000;"></span><span style="color: #0000ff;">var</span><span style="color: #000000;">articleList</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">Array();<br>
articleList[</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">article(</span><span style="color: #000000;">"</span><span style="color: #000000;">http://www.cnblogs.com/JustinYoung/archive/2007/10/31/dead-of-typer.html</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">利用《死亡打字员》提高程序员的命根子技能</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>
articleList[</span><span style="color: #000000;">1</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">article(</span><span style="color: #000000;">"</span><span style="color: #000000;">http://www.cnblogs.com/JustinYoung/archive/2007/11/08/952833.html</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">【CSS翻转门】技术实例讲解(附源码下载)</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>
articleList[</span><span style="color: #000000;">2</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">article(</span><span style="color: #000000;">"</span><span style="color: #000000;">http://www.cnblogs.com/JustinYoung/archive/2007/11/20/speeding-up-web-site-14rule.html</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">如何提高网页的效率(上篇)</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>
articleList[</span><span style="color: #000000;">3</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">article(</span><span style="color: #000000;">"</span><span style="color: #000000;">http://www.cnblogs.com/JustinYoung/archive/2007/11/28/speeding-up-web-site-yslow.html</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">如何提高网页的效率(下篇)</span><span style="color: #000000;">"</span><span style="color: #000000;">);</span>
</div>
<h5>3:调用对象方法,完成添加</h5>
<div class="floatRPic">
<a title="javascript的面向对象" href="http://justinyoung.cnblogs.com/" target="_blank"><img alt="web标准设计" src="http://images.cnblogs.com/cnblogs_com/justinyoung/2007/commendArticle.jpg"></a><br>
完成的效果图</div>
<p>在window.onload的时候,从数组里面随机选出一个元素,然后调用这个元素(对象)的commendMe方法,从而完成添加。</p>
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 427px; padding-top: 4px; border-bottom: #cccccc 1px solid; height: 111px; background-color: #eeeeee;">
<span style="color: #000000;">window.onload</span><span style="color: #000000;">=</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){<br></span><span style="color: #0000ff;">var</span><span style="color: #000000;">randomNum</span><span style="color: #000000;">=</span><span style="color: #000000;">parseInt(articleList.length</span><span style="color: #000000;">*</span><span style="color: #000000;">Math.random());<br>
articleList[randomNum].commendMe();<br>
articleList</span><span style="color: #000000;">=</span><span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">释放资源</span><span style="color: #008000;"><br></span><span style="color: #000000;">}</span>
</div>
<p>你可以通过,查看本页的源代码清楚的看到这些代码片断。</p>
<h4>另记:</h4>
<p>周末,花了一些时间,修正了一下,博客的侧边栏代码和底部代码,在Firefox里面终于没有脚本错误了(主要就是Firefox对insertAdjacentElement支持的不好惹得祸,换成appendChild就好了)。这下,在Firefox下也能完美的浏览了。嘿嘿~(窃喜中……)</p>
<hr align="left" width="80%">
Tag标签:<span style="font-size: 80%; color: #666;">javascript 面向对象,面向对象的javascript,javascript 对象,javascript的对象,javascript中的对象,javascript对象详解,javascript缺少对象,javascript面向对象程序设计,javascript prototype,firefox insertAdjacentElement,appendChild</span>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics