[js] prototype chain
最近在看本书 pro javascript,受益匪浅。
在看closure这章的时候,作者推荐 http://jibbering.com/faq/faq_notes/closures.html 这篇文章。
很不错的一篇文章,很深入,所以看得很慢。看到有段代码有点意思,是关于prototype chain的:
1
/* A "constructor" function for creating objects of a -
2
MyObject1 - type.
3
*/
4
function MyObject1(formalParameter){
5
/* Give the constructed object a property called - testNumber - and
6
assign it the value passed to the constructor as its first
7
argument:-
8
*/
9
this.testNumber = formalParameter;
10
}
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
/* A "constructor" function for creating objects of a -
13
MyObject2 - type:-
14
*/
15
function MyObject2(formalParameter){
16
/* Give the constructed object a property called - testString -
17
and assign it the value passed to the constructor as its first
18
argument:-
19
*/
20
this.testString = formalParameter;
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
/* The next operation replaces the default prototype associated with
24
all MyObject2 instances with an instance of MyObject1, passing the
25
argument - 8 - to the MyObject1 constructor so that its -
26
testNumber - property will be set to that value:-
27
*/
28
MyObject2.prototype = new MyObject1( 8 );
29![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
/* Finally, create an instance of - MyObject2 - and assign a reference
31
to that object to the variable - objectRef - passing a string as the
32
first argument for the constructor:-
33
*/
34![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
var objectRef = new MyObject2( "String_Value" );
36![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)