javascript 클래스 의 계승 3

1828 단어 JavaScript
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
<!---
      ,             extend   
-->
 <body>
	<script>

		//     ,         
		function extend2(subClass, superClass) {
			var F = function() {};
			F.prototype = superClass.prototype;
			subClass.prototype = new F();
			subClass.prototype.constructor = subClass;
			
			//    superClass  
			subClass.superClass = superClass.prototype;
			if(superClass.prototype.constructor == Object.prototype.constructor) {
				superClass.prototype.constructor = superClass;//     
			}
		}


		function Person(name) {
			this.name = name;
		}

		Person.prototype.getName = function() {
			return this.name;
		}

		function Author(name, books) {
			Author.superClass.constructor.call(this,name);//call the superclass's constructor
			this.books = books;
		}

		extend2(Author,Person);

		Author.prototype.getBooks = function() {
			return this.books;
		}

		var author = new Author('Dustin Diaz',['javascript design patterns ']);
		alert(author.getName());
		alert(author.getBooks());

	</script>
 </body>
</html>

좋은 웹페이지 즐겨찾기