1
+ //Date类扩展一个类方法
2
+ Date . prototype . shortFormat = function ( ) {
3
+ return ( this . getMonth ( ) + 1 ) + "/" + this . getDate ( ) + "/" + this . getFullYear ( ) ;
4
+
5
+ } ;
6
+
7
+ //Blog类的初始化方法
8
+ function Blog ( body , date , image ) {
9
+ this . body = body ;
10
+ this . date = date ;
11
+ this . image = image ;
12
+ }
13
+ //Blog类的实例方法,所有实例对象都只有这么一份
14
+ Blog . prototype . toString = function ( ) {
15
+ return "[" + this . date . shortFormat ( ) + "]" + this . body ;
16
+ } ;
17
+ Blog . prototype . toHTML = function ( highlight ) {
18
+ var blogHTML = "" ;
19
+ blogHTML += highlight ? "<p style='background-color:#EEEEEE'>" : "<p>" ;
20
+ //blog对象是否有image元素
21
+ if ( this . image ) {
22
+ blogHTML += "<strong>" + this . date . shortFormat ( ) + "</strong><br /><table><tr><td><img src='" +
23
+ this . image + "'/></td><td style='vertical-align:top'>" + this . body + "</td></tr></table><em>" +
24
+ this . signature + "</em></p>" ;
25
+ }
26
+ else {
27
+ blogHTML += "<strong>" + this . date . shortFormat ( ) + "</strong><br />" + this . body +
28
+ "<br /><em>" + this . signature + "</em></p>" ;
29
+ }
30
+ return blogHTML ;
31
+ } ;
32
+ Blog . prototype . containsText = function ( text ) {
33
+ return ( this . body . toLowerCase ( ) . indexOf ( text . toLowerCase ( ) ) != - 1 ) ;
34
+ } ;
35
+
36
+ //添加一个类属性,所有的Blog对象分享一份。
37
+ Blog . prototype . signature = "签名:黄成都你还" ;
38
+
39
+ //类方法。这个方法只有Blog类可以调用。Blog类的对象也不可以调用,而且不能访问Blog的成员属性
40
+ Blog . blogSorter = function ( blog1 , blog2 ) {
41
+ return blog2 . date - blog1 . date ;
42
+ } ;
43
+
44
+
45
+ var blog = [ new Blog ( "Got the new cube I ordered. It's a real pearl." , new Date ( "08/14/2008" ) ) ,
46
+ new Blog ( "Solved the new cube but of course, now I'm bored and shopping for a new one." , new Date ( "08/19/2008" ) ) ,
47
+ new Blog ( "Managed to get a headache toiling over the new cube. Gotta nap." , new Date ( "08/16/2008" ) ) ,
48
+ new Blog ( "Found a 7x7x7 cube for sale online. Yikes! That one could be a beast." , new Date ( "08/21/2008" ) ) ,
49
+ new Blog ( "Met up with some fellow cubers to discuss the prospect of a 7x7x7 cube. Mixed feelings." , new Date ( "08/29/2008" ) ) ,
50
+ new Blog ( "Went ahead and ordered the scary 7x7x7 cube. Starting a mental exercise regimen to prepare." , new Date ( "09/01/2008" ) ) ,
51
+ new Blog ( "Attended a rally outside of a local toy store that stopped carrying cube puzzles. Power to the puzzlers!" , new Date ( "09/03/2008" ) ) ,
52
+ new Blog ( "Got the new 7x7x7 cube. Could be my last blog post for a while..." , new Date ( "09/05/2008" ) ) ,
53
+ new Blog ( "Wow, it took me a couple of weeks but the new cube is finally solved!" , new Date ( "09/19/2008" ) , "cube777.png" ) ] ;
54
+
55
+ // Show the list of blog entries
56
+ function showBlog ( numEntries ) {
57
+ // First sort the blog
58
+ blog . sort ( Blog . blogSorter ) ;
59
+
60
+ // Adjust the number of entries to show the full blog, if necessary
61
+ if ( ! numEntries )
62
+ numEntries = blog . length ;
63
+
64
+ // Show the blog entries
65
+ var i = 0 , blogListHTML = "" ;
66
+ while ( i < blog . length && i < numEntries ) {
67
+ blogListHTML += blog [ i ] . toHTML ( i % 2 == 0 ) ;
68
+ i ++ ;
69
+ }
70
+
71
+ // Set the blog HTML code on the page
72
+ document . getElementById ( "blog" ) . innerHTML = blogListHTML ;
73
+ }
74
+
75
+ // Search the list of blog entries for a piece of text
76
+ function searchBlog ( ) {
77
+ var searchText = document . getElementById ( "searchtext" ) . value ;
78
+ for ( var i = 0 ; i < blog . length ; i ++ ) {
79
+ // See if the blog entry contains the search text
80
+ if ( blog [ i ] . containsText ( searchText ) ) {
81
+ alert ( blog [ i ] ) ;
82
+ break ;
83
+ }
84
+ }
85
+
86
+ // If the search text wasn't found, display a message
87
+ if ( i == blog . length )
88
+ alert ( "Sorry, there are no blog entries containing the search text." ) ;
89
+ }
90
+ // Display a randomly chosen blog entry
91
+ function randomBlog ( ) {
92
+ // Pick a random number between 0 and blog.length - 1
93
+ var i = Math . floor ( Math . random ( ) * blog . length ) ;
94
+ alert ( blog [ i ] ) ;
95
+ }
0 commit comments