﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-溜金岁月, 成长点滴-随笔分类-AJAX相关</title><link>http://www.blogjava.net/javabloger/category/11545.html</link><description>no description</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 03:18:43 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 03:18:43 GMT</pubDate><ttl>60</ttl><item><title>理解prototype </title><link>http://www.blogjava.net/javabloger/archive/2006/05/26/48330.html</link><dc:creator>liunix</dc:creator><author>liunix</author><pubDate>Fri, 26 May 2006 06:46:00 GMT</pubDate><guid>http://www.blogjava.net/javabloger/archive/2006/05/26/48330.html</guid><wfw:comment>http://www.blogjava.net/javabloger/comments/48330.html</wfw:comment><comments>http://www.blogjava.net/javabloger/archive/2006/05/26/48330.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/javabloger/comments/commentRss/48330.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/javabloger/services/trackbacks/48330.html</trackback:ping><description><![CDATA[
		<p>
		</p>
		<p>prototype 是javascript实现继承的基本方法，但这种继承和java等继承有什么本质的区别呢<br /><br /><br />在 JavaScript 中，对象的 prototype 在运行期是不可见的，只能在定义对象的构造函数时，创建对象之前设定。下面的用法都是错误的：</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%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #000000">o2.prototype </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> o1;<br /></span>
				<span style="COLOR: #008000">/*</span>
				<span style="COLOR: #008000">*<br />  这时只定义了 o2 的一个名为“prototype”的属性，<br />  并没有将 o1 设为 o2 的 prototype。<br /></span>
				<span style="COLOR: #008000">*/</span>
				<span style="COLOR: #000000">
						<br /> <br /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> ---------------</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #000000"> <br />f2 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">function</span>
				<span style="COLOR: #000000">(){};<br />o2 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> f2;<br />f2.prototype </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> o1;<br /></span>
				<span style="COLOR: #008000">/*</span>
				<span style="COLOR: #008000">*<br />  这时 o1 并没有成为 o2 的 prototype，<br />  因为 o2 在 f2 设定 prototype 之前已经被创建。<br /></span>
				<span style="COLOR: #008000">*/</span>
				<span style="COLOR: #000000">
						<br /> <br /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> ---------------</span>
				<span style="COLOR: #008000">
						<br />
				</span>
				<span style="COLOR: #000000"> <br />f1 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">function</span>
				<span style="COLOR: #000000">(){};<br />f2 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">function</span>
				<span style="COLOR: #000000">(){};<br />o1 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> f1;<br />f2.prototype </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> o1;<br />o2 </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> f2;<br /></span>
				<span style="COLOR: #008000">/*</span>
				<span style="COLOR: #008000">*<br />  同样，这时 o1 并不是 o2 的 prototype，<br />  因为 JavaScript 不允许构造函数的 prototype 对象被其它变量直接引用。<br /></span>
				<span style="COLOR: #008000">*/</span>
				<span style="COLOR: #000000">
						<br />
				</span>
		</div>
		<p>从上面的例子可以看出：如果你想让构造函数 F2 继承另外一个构造函数 F1 所定义的属性和方法，那么你必须先创建一个 F1 的实例对象，并立刻将其设为 F2 的 prototype。笔者认为这种继承方法不够直观，<font color="#ff0000"><strong>并且将对象的设计期和运行期混在一起</strong>。</font><br /><br /><br />就是拿类来“继承”另外一个类的实例的说。。。<br /><br /><br /><br />继续：<br /></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%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">function</span>
				<span style="COLOR: #000000"> MyObject1(formalParameter){<br />    </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.testNumber </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> formalParameter;<br />}<br /><br /></span>
				<span style="COLOR: #0000ff">function</span>
				<span style="COLOR: #000000"> MyObject2(formalParameter){<br />    </span>
				<span style="COLOR: #0000ff">this</span>
				<span style="COLOR: #000000">.testString </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> formalParameter;<br />}<br /><br />MyObject2.prototype </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> MyObject1( </span>
				<span style="COLOR: #000000">8</span>
				<span style="COLOR: #000000"> );<br /><br /></span>
				<span style="COLOR: #0000ff">var</span>
				<span style="COLOR: #000000"> objectRef </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">new</span>
				<span style="COLOR: #000000"> MyObject2( </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">String_Value</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000"> );</span>
		</div>
		<br />The<font color="#ff0000"></font><font color="#0000ff">instance of <code>MyObject2</code></font> referred to by the <code>objectRef</code> variable has a prototype chain. The <font color="#ff0000">first object in that chain is the instance of <code>MyObject1</code></font> that was created and assigned to the prototype property of the <code>MyObject2</code> constructor. The <font color="#ff0000"><font color="#0000ff">instance of <code>MyObject1</code></font> has a prototype, the default Object prototype that corresponds with the object referred to by <code>Object.prototype</code>. <font color="#0000ff"><code>Object.prototype</code> has a null prototype(!)</font> so the prototype chain comes to an end at this point. <br /></font><br /><br />说明prototype是类的一个属性而不是实例的属性。。。<br /><br /><br /><br /><br />prototype是一个链，链的尽头是Object.prototype而这个prototype有一些toString()之类的通用方法，且这个prototype的prototype为null<br /><br /><br />现在了解了怎样通过prototype实现继承了吧：<br /><br />1，如果给一个实例动态添加属性的话，这个属性只能做用于这个实例<br />2，如果给一个类的prototype添加属性的话，这个属性会在类的每个实例中产生一个拷贝，即会做用于类的所有实例，产生类似继承的效果<br /><img src ="http://www.blogjava.net/javabloger/aggbug/48330.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/javabloger/" target="_blank">liunix</a> 2006-05-26 14:46 <a href="http://www.blogjava.net/javabloger/archive/2006/05/26/48330.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>