JS使用prototype的方法

来源:爱站网时间:2022-08-03编辑:网友分享
本篇文章主要给大家讲述下JS使用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+"
"+"[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"></DIV>
<DIV class=code>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);
 }
}</DIV>
<DIV class=code>var person = new Person("robbin",30);
person.toString();
</script>
</body>
</html>

 

JS使用prototype的方法内容不知道朋友们都了解清楚了没有,在我们爱站技术频道网站有很多精彩的技术文章,如果你有需要的话可以前来参考阅读。

上一篇:调用另类flash的方法

下一篇:js利用document.selection判断文本或对象

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载