Web effects to create their own classes and objects, we should be mastered, we all know that the properties of the object in JavaScript can be dynamically defined after the object is created, such as the following code:
The code is as follows:
<script type= "Text/javascript" >
Defined
var ocar = new Object ();
Ocar.color = "Red";
Ocar.doors = 4;
Ocar.showcolor = function () {
alert (This.color);
}
Call
Ocar.showcolor ();
</script>
It's easy to use Ocar objects, but we create multiple car instances. We can use a function to encapsulate the code above to implement the
: <script type= "Text/javascript" >
The code is as follows:
Defined
function Createcar () {
var ocar = new Object ();
Ocar.color = "Red";
Ocar.doors = 4;
Ocar.showcolor = function () {
alert (This.color);
}
return ocar;
}
Call
var ocar1 = Createcar ();
var ocar2 = Createcar ();
Ocar1.color = "BLACK";
Ocar1.showcolor ();
Ocar2.showcolor ();
</script>