|
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()來創(chuàng)建一個對象類型,然后定義該對象類型,注意initialize方法是Person的構(gòu)造器,完整的HTML如下:
復(fù)制代碼 代碼如下:
<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>
JavaScript技術(shù):JavaScript使用prototype定義對象類型,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。