JavaScript class definitions and functions multiple methods (1/5)

Source: Internet
Author: User

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>

Home 1 2 3 4 5 last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.