Home  >  Article  >  Web Front-end  >  Summary of Javascript custom type methods_javascript skills

Summary of Javascript custom type methods_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:33:331032browse
1. Define type
Copy code The code is as follows:

function UserObject (parameter) {
}

parameter can be omitted, which is equivalent to the constructor parameter in C#.
2. Instantiate the custom type
Copy the code The code is as follows:



3. Add attributes
Copy code The code is as follows:

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

//Use
to copy the code The code is as follows :

<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. Add method (circle class )
Copy code The code is as follows:

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

Associated to the custom type:
Copy code The code is as follows:



Use custom method:
Copy code The code is as follows:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn