JavaScript使用prototype定义对象类型(转)[分享


From: JavaEye.com

prototype提供了一套JavaScript面向对象基础设施,我们可以使用它来进行面向对象编程,定义对象类型方式如下:

var Person = Class.create();
Person.prototype = {
initialize : function(name, age) {
this.name = name;
this.age = age;
},
toString : function() {
document.writeln(“[name]:”+this.name+”<br>”+”[age]:”+this.age);
}
}

先使用Class.create()来创建一个对象类型,然后定义该对象类型,注意initialize方法是Person的构造器,完整的HTML如下:

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Test Object</title>
<script type=”text/javascript” src=”prototype.js”></script>
</head>
<body>
<script type=”text/javascript”>

var Person = Class.create();
Person.prototype = {
initialize : function(name, age) {
this.name = name;
this.age = age;
},
toString : function() {
document.writeln(“[name]:”+this.name+”<br>”+”[age]:”+this.age);
}
}

var person = new Person(“robbin”,30);
person.toString();
</script>
</body>
</html>

—-想了解更多的linux相关异常处理怎么解决关注<计算机技术网(www.ctvol.com)!!>



本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/jspttutorial/64799.html

(0)
上一篇 2020年4月11日
下一篇 2020年4月11日

精彩推荐