JS中如何创建对象
发布日期:2021-05-07 23:14:13 浏览次数:13 分类:原创文章

本文共 622 字,大约阅读时间需要 2 分钟。

JS中创建对象并用prototype属性动态扩展方法

<!DOCTYPE html><html>	<head>		<meta charset="UTF-8">		<title></title>	</head>	<body>		<script type="text/javascript">			Student = function(sname,sno,sage){   				this.sname=sname;				this.sno=sno;				this.sage=sage;			    this.getSage = function(){   			 		return this.sage;				}			}   //定义学生类,有姓名,学号,年龄属性,以及获得年龄方法			var stu=new Student("czx",2018,22);			alert(stu.sname);			alert(stu.sno);			alert(stu.getSage());						Student.prototype.getSname= function(){   				return this.sname;			}  //使用Object超类的prototype属性对原本定义类属性及方法,进行动态扩展			alert("这是动态扩展的方法"+stu.getSname());		</script>	</body></html>
上一篇:正则表达式初识
下一篇:Spring整合mybatis用阿里连接池的配置错误

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月21日 07时51分43秒