Javascript Oop-[reprint]

Source: Internet
Author: User
Tags getcolor
From: http://www.matrix.org.cn/blog/liu2liu2/
1. What is JavaScript?
Javascript is a descriptive scripting language that can be freely embedded into HTML files. What can I do with JavaScript? The function is very simple, that is, to process or initialize the webpage for the event currently triggered by the browser. It is written on the webpage in advance Code (Or "script"), and the code is then transmitted together with the HTML file to the browser on the client, where the Code is interpreted and executed, during the execution, the server was not assisted by the driver, which reduced the burden on the server.
Many programming experts look down on JavaScript programming and think it is just a child's stuff. Indeed, JavaScript, as a scripting language, is much simpler than other languages, without complex syntax and complex architecture.

2. object-oriented programming)
A world can be completely composed of objectsAlgorithmThe world based on is represented only by objects, and subsequent code is written. This programming method is called the object-oriented programming idea.
Three elements of OOP:
1. Encapsulation
2. Inheritance
3. Polymorphism

3. How does JavaScript implement OOP
3.1. encapsulation (WRAP)
JavaScript Object encapsulation relies mainly on functions. The following is a simple example:
//*********************************** * *********
// define pet) object
//********************************** * **********
function PET () {
// name
This. name = NULL;
// color
This. color = NULL;
// obtain the name
This. getname = function () {
return this. name;
};
// set the name
This. setname = function (newname) {
This. name = newname;
};
// obtain the color
This. getcolor = function () { return this. color;
};
// set the color
This. setcolor = function (newcolor) {
This. color = newcolor;
};
// define a method to be implemented
This. getfood = NULL;
// get the pet description
This. tostring = function () {
return "the PET is" + this. name + ", it's" + this. color + ", and it likes" + this. getfood () + ". ";
};< BR >}< br> 3.2. inheritance
the implementation of JavaScript inheritance mainly relies on prototype. Below is a subclass of the pet class.

//************************************** *******
// Define a cat object
//************************************** *******
Function CAT (){
// Implement the getfood method defined in PET
This. getfood = function (){
Return "fish ";
};
}

// Declare the cat prototype, that is, the cat parent class
Cat. Prototype = new pet;

Multi-level inheritance
//************************************** *******
// Define the persiancat (Persian cat) object
//************************************** *******
Function persiancat (){
}

// Declare the prototype of persiancat, that is, the parent class of persiancat
Persiancat. Prototype = new cat;
3.3. Override and Polymorphism)
// Reload the tostring method of PET
Persiancat. Prototype. tostring = function (){
Return "it's just a persian cat .";
};

Note: The difference between override and overload is that JavaScript does not support overload because ...... Think about it yourself.

Javascript is a weak language, so do not compare it with C ++, Java, and other languages.
4. appendix
4.1. complete example
4.1.1. pet. JS
//********************************** * **********
// define pet) object
//********************************** * **********
function PET () {
// name
This. name = NULL;
// color
This. color = NULL;
// obtain the name
This. getname = function () {
return this. name;
};
// set the name
This. setname = function (newname) {
This. name = newname;
};
// obtain the color
This. getcolor = function () {
return this. color;
};
// set the color
This. setcolor = function (newcolor) {
This. color = newcolor;
};
// define a method to be implemented
This. getfood = NULL;
// get the pet description
This. tostring = function () {
return "the PET is" + this. name + ", it's" + this. color + ", and it likes" + this. getfood () + ". ";
};
}

//************************************** *******
// Define a cat object
//************************************** *******
Function CAT (){
// Implement the getfood method defined in PET
This. getfood = function (){
Return "fish ";
};
}

// Declare the cat prototype, that is, the cat parent class
Cat. Prototype = new pet;

//************************************** *******
// Define the persiancat (Persian cat) object
//************************************** *******
Function persiancat (){
}

// Declare the prototype of persiancat, that is, the parent class of persiancat
Persiancat. Prototype = new cat;

// Reload the tostring method of PET
Persiancat. Prototype. tostring = function (){
Return "it's just a persian cat .";
};

//************************************** *******
// Define the dog object
//************************************** *******
Function dog (){
// Implement the getfood method defined in PET
This. getfood = function (){
Return "bone ";
};
}

// Declare the prototype of the dog, that is, the parent class of the dog.
Dog. Prototype = new pet;
4.1.2. pet.htm
<Script language = "JavaScript" src = "Pet. js">
</SCRIPT>
<Script language = "JavaScript">
// Define a cat object
VaR cat = new CAT ();
Cat. setname ("Mimi ");
Cat. setcolor ("white ");

// Define a dog object
VaR dog = new dog ();
Dog. setname ("Wangwang ");
Dog. setcolor ("yellow ");

// Define a persiancat object
VaR persiancat = new persiancat ();

// Define an array and save the preceding three objects
VaR pets = new array (3 );
Pets [0] = cat;
Pets [1] = dog;
Pets [2] = persiancat;

// TestProgram
For (VAR I = 0, size = pets. length; I <size; I ++ ){
Alert (PETS [I]. tostring ());
}
</SCRIPT>
4.1.3. Running result

The following three dialog boxes are displayed:
The pet is Mimi, it's white, and it likes fish.
The pet is Wangwang, it's yellow, and it likes bone.
It's just a persian cat.
This example is passed in ie6.0.


Related Article

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.