js 使用建構函式和原型鏈實現繼承操作

HappyCodings發表於2020-11-11
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>index</title>
		<script type="text/javascript">
			function Father(name,age){
				this.name = name,
				this.age = age
			}
			Father.prototype.sayWhois = function(name){
				console.log(this.name)
			}
			Sun.prototype = new Father();
			Sun.prototype.constructor = Sun;
			function Sun(name,age,jj){
				Father.call(this,name,age);
				this.jj = jj;
			}
			var f = new Father('baba',22);
			var erzi = new Sun('erzi',2,'jj');
			erzi.sayWhois();
		</script>
	</head>
	<body>
		webDemo
	</body>
</html>

相關文章