JavaScript使用prototype定義對象類型(轉)[
更新時間:2006年12月22日 00:00:00 作者:
From: JavaEye.com
prototype提供了一套JavaScript面向對象基礎設施,我們可以使用它來進行面向對象編程,定義對象類型方式如下:
先使用Class.create()來創(chuàng)建一個對象類型,然后定義該對象類型,注意initialize方法是Person的構造器,完整的HTML如下:
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);
}
}
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的構造器,完整的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>
<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>
您可能感興趣的文章:
- 判斷js中各種數(shù)據(jù)的類型方法之typeof與0bject.prototype.toString講解
- js類定義函數(shù)時用prototype與不用的區(qū)別示例介紹
- JavaScript使用prototype定義對象類型
- JavaScript類和繼承 prototype屬性
- JavaScript子類用Object.getPrototypeOf去調用父類方法解析
- javascript基于prototype實現(xiàn)類似OOP繼承的方法
- JS偽繼承prototype實現(xiàn)方法示例
- 淺談js構造函數(shù)的方法與原型prototype
- js實現(xiàn)prototype擴展的方法(字符串,日期,數(shù)組擴展)
- Javascript中prototype屬性實現(xiàn)給內(nèi)置對象添加新的方法
- JS利用prototype給類添加方法操作詳解
相關文章
用js+iframe形成頁面的一種遮罩效果的具體實現(xiàn)
用js形成頁面的一種遮罩效果,選擇想要進行遮罩的窗口,在這里想要遮罩的是一個iframe窗口,具體的實現(xiàn)如下,感興趣的朋友可以參參考下2013-12-12
JS構造一個html文本內(nèi)容成文件流形式發(fā)送到后臺
本文通過實例代碼給大家介紹了JS構造一個html文本內(nèi)容成文件流形式發(fā)送到后臺的相關資料,需要的朋友可以參考下2018-07-07
JS提示:Uncaught SyntaxError: Unexpected token ILLEGAL錯誤的解決方法
這篇文章主要介紹了JS提示:Uncaught SyntaxError: Unexpected token ILLEGAL錯誤的解決方法,涉及針對字符串參數(shù)的處理方法,需要的朋友可以參考下2016-08-08

