我是靠谱客的博主 炙热黑裤,这篇文章主要介绍在Javascript中定义对象类别,现在分享给大家,希望可以做个参考。

From: JavaEye.com

注意JavaScript中对象类别的定义,使用function来定义对象类别,初始化对象使用new操作符
function Person(name, age) {
 this.name = name;
 this.age = age;
 this.toString = function() {
 document.writeln("[name]:"+this.name+"<br>"+"[age]:"+this.age);
 }
}

完整的简单HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JavaScript</title>
<script language="javascript" type="text/javascript">
function Person(name, age) {
 this.name = name;
 this.age = age;
 this.toString = function() {
 document.writeln("[name]:"+this.name+"<br>"+"[age]:"+this.age);
 }
}
</script>
</head>

<body>
<script type="text/javascript">
 var person = new Person();
 person.name="robbin";
 person.age=30;
 person.toString();
</script>
</body>
</html>

最后

以上就是炙热黑裤最近收集整理的关于在Javascript中定义对象类别的全部内容,更多相关在Javascript中定义对象类别内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(227)

评论列表共有 0 条评论

立即
投稿
返回
顶部