手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JavaScript使用prototype定义对象类型
JavaScript使用prototype定义对象类型
摘要:From:JavaEye.comprototype提供了一套JavaScript面向对象基础设施,我们可以使用它来进行面向对象编程,定义对象...

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

varPerson=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>

<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>

<title>TestObject</title>

<scripttype="text/javascript"src="prototype.js"></script>

</head>

<body>

<scripttype="text/javascript"></DIV>

<DIVclass=code>varPerson=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>

<DIVclass=code>varperson=newPerson("robbin",30);

person.toString();

</script>

</body>

</html>

【JavaScript使用prototype定义对象类型】相关文章:

JavaScript实现Iterator模式实例分析

JavaScript的while循环的使用

JavaScript中toString()方法的使用详解

JavaScript使用addEventListener添加事件监听用法实例

JavaScript中反正弦函数Math.asin()的使用简介

JavaScript里实用的原生API汇总

JavaScript对传统文档对象模型的支持

JavaScript中的sub()方法的使用介绍

JavaScript中string对象

JavaScript函数使用的基本教程

精品推荐
分类导航