This paper is the official HTML5 training course for Brother Lian it education organization, mainly introduces: HTML5 Mobile Development Road (2)--javascript review
JavaScript Object-Oriented basics
1, how to define a class, use the following syntax to create a class
[JavaScript] view plain copy
Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>
function person (name, age) { //used to capitalize the first letter
//this modified variables are called Properties
this.name = name;
this.age = age;
//If the property value is a function, then this property is a method
This.play = function () {
Alert (' play football ... ');
};
}
2. How to create an instance of this class
[JavaScript] view plain copy
Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>
var p = New Person (' ZS ', 22);
P.play ();
P.name;
3. Two other ways to create JavaScript objects
(1) First create an instance of type object and then add new properties and methods
JavaScript is a dynamic language that can add new properties and methods to objects at run time
[JavaScript] view plain copy
Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>
var obj = new Object ();
Obj.name = ' ZS ';
Obj.age = 22;
Obj.play = function () {
Alert (' play ... ');
};
(2) using the "JSON" syntax
[JavaScript] view plain copy
Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>
var p = {' name ':' Zs ',' age ': 22};
Ar p = {' name ':' Zs ',' play ':function () {
Alert (' hello ');
};
Or
[JavaScript] view plain copy
Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>
var p = {name:' Zs ', Age:22,marrid:false};
var p = {name:' Zs ', Play:function () {
Alert (' hello ');
}};
Property value if the string must be enclosed in quotation marks (single-pair)
Property value allows Number,string,boolean,null,object
[JavaScript] view plain copy
Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>
var p = {name:' Zs ',
address:{
City:' Beijing ',
Street:' CA '
}
};
A complete example
[HTML] view plain copy
Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>
<html>
<head>
<script>
Function F1 () {
var p = {' name ': ' Zs ', ' age ': 22};
var p = {name: ' Zs ', age:22,marrid:false};
var p = {name: ' Zs ',
address:{
City: ' Beijing ',
Street: ' CA '
}
};
alert (p.name);
alert (p.address.city);
}
function F2 () {
var arr = [
{' name ': ' Zs ', ' age ': 22},
{' name ': ' ls ', ' Age ': 32}
];
alert (arr[1].name);
}
</Script>
</head>
<body>
<input type="button" value= "clickMe" onclick="F2 ();" />
</Body>
</html>
HTML5 Mobile Development Road (--javascript) Review 2