国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Javascript 自定義類型方法小結(jié)

2019-11-21 00:42:49
字體:
供稿:網(wǎng)友
1. 定義類型
復(fù)制代碼 代碼如下:

function UserObject(parameter) {
}

parameter 可省略,相當(dāng)于C#中構(gòu)造函數(shù)參數(shù)。
2. 實(shí)例化自定義類型
復(fù)制代碼 代碼如下:

<script type="text/javascript">
function userobject(parameter){
}
//myobject is now an object of type userobject!
var myobject=new userobject("hi")
alert(myobject)
</script>

3. 添加屬性
復(fù)制代碼 代碼如下:

function userobject(parameter){
this.firstproperty=parameter
this.secondproperty="This is the second property"
}

//使用
復(fù)制代碼 代碼如下:

<script>
var myobject=new userobject("hi there.")
//alerts "hi there."
alert(myobject.firstproperty)
//writes "This is the second property"
document.write(myobject.secondproperty)
</script>

4.添加方法 (circle類)
復(fù)制代碼 代碼如下:

//first method function
function computearea(){
var area=this.radius*this.radius*3.14
return area
}
//second method function
function computediameter(){
var diameter=this.radius*2
return diameter
}

關(guān)聯(lián)到自定義類型:
復(fù)制代碼 代碼如下:

<script type="text/javascript">
/*the below creates a new object, and gives it the two methods defined earlier*/
function circle(r){
//property that stores the radius
this.radius=r
this.area=computearea
this.diameter=computediameter
}
</script>

使用自定義方法:
復(fù)制代碼 代碼如下:

<script type="text/javascript">
var mycircle=new circle(20)
//alerts 1256
alert("area="+mycircle.area())
//alerts 400
alert("diameter="+mycircle.diameter())
</script>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海伦市| 卫辉市| 惠州市| 加查县| 临安市| 娄底市| 青川县| 故城县| 嘉定区| 得荣县| 仙桃市| 德钦县| 安泽县| 云阳县| 洪洞县| 安康市| 抚松县| 沅陵县| 峨山| 长寿区| 古丈县| 兴宁市| 沂水县| 南昌县| 出国| 兴海县| 周口市| 大兴区| 平潭县| 怀安县| 壤塘县| 双辽市| 巧家县| 聊城市| 麟游县| 平定县| 秭归县| 黎川县| 四平市| 深州市| 桦南县|