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="/UploadFiles/2021-04-02/prototype.js"></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>
标签:
JavaScript使用prototype定义对象类型(转)[

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“JavaScript使用prototype定义对象类型(转)[”

暂无“JavaScript使用prototype定义对象类型(转)[”评论...