Heim  >  Artikel  >  Web-Frontend  >  Javascript 自定义类型方法小结_javascript技巧

Javascript 自定义类型方法小结_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:33:331012Durchsuche
1. 定义类型
复制代码 代码如下:

function UserObject(parameter) {
}

parameter 可省略,相当于C#中构造函数参数。
2. 实例化自定义类型
复制代码 代码如下:



3. 添加属性
复制代码 代码如下:

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

//使用
复制代码 代码如下:

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

4.添加方法 (circle类)
复制代码 代码如下:

//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
}

关联到自定义类型:
复制代码 代码如下:



使用自定义方法:
复制代码 代码如下:


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn