標籤:javascript繼承 原型繼承 借用建構函式繼承 混合繼承
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘test3.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script src="js/jquery-2.1.1.min.js"></script><script type="application/javascript">//js中實現繼承,採用原型鏈的概念//1.建構函式.prototype=原型對象//2.原型對象.constructor=建構函式(模板)//3.原型對象.isPropertyOf(執行個體對象)判斷執行個體對象的原型是不是當前對象//4.建構函式,執行個體對象(類和執行個體)//父類建構函式 supfunction Sup(name){this.name=name;}Sup.prototype={constructor:Sup,sayName:function(){alert(this.name);}} //子類建構函式 Subfunction Sub(age){this.age=age;}//如果讓子類的原型對象,結果會怎麼樣?(實行js繼承)//此時的原型對象包含一個指向另一個原型的指標//相應的;另一個原型中也包含著一個指向另一個建構函式的指標//子類的原型對象的構造器變成了父類的構造器//Sub.prototype=new Sup();//alert(Sub.prototype.constructor); //父類構造器Sub.prototype=new Sup(‘張三‘);var sub1=new Sub();alert(sub1.name); //張三sub1.sayName();</script> </head> <body> This is my JSP page. <br> </body></html>
三種繼承方式:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘test3.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script src="js/jquery-2.1.1.min.js"></script><script type="application/javascript">//三種繼承方式//原型繼承/*function Person(name,age){this.name=name;this.age=age;}//父類原型對象屬性Person.prototype.id=10;function Boy(sex){this.sex=sex;}Boy.prototype=new Person(‘張三‘,21);var b=new Boy();alert(b.id); //10//原型繼承的特點:既繼承了父類的模板,又繼承了父類的原型對象*///類繼承:只繼承模板,不繼承原型對象(借用建構函式的方式繼承)/*function Person(name,age){this.name=name;this.age=age;}//父類原型對象屬性Person.prototype.id=10;function Boy(name,age,sex){Person.call(this,name,age); //綁定對象this.sex=sex;}var b=new Boy(‘張三‘,23,‘男‘);alert(b.age);alert(b.name);alert(b.sex);alert(b.age); //undefined 父類原型對象並沒有繼承 *///原型繼承+借用建構函式繼承=混合繼承function Person(name,age){this.name=name;this.age=age;}//父類原型對象屬性Person.prototype.id=10;Person.prototype.sayName=function(){alert(this.name);}function Boy(name,age,sex){Person.call(this,name,age); //1.借用建構函式繼承this.sex=sex;}//只剩下父類的執行個體和弗雷德原型對象的關係了Boy.prototype=new Person(); //2.原型繼承,繼承父類的原型對象var boy=new Boy(‘張三‘,21,‘男‘);alert(boy.id);boy.sayName();</script> </head> <body> This is my JSP page. <br> </body></html>
本文出自 “matengbing” 部落格,請務必保留此出處http://matengbing.blog.51cto.com/11395502/1879585
javascript繼承,原型繼承,借用建構函式繼承,混合繼承