Javascript object-oriented features and javascript object-oriented

Source: Internet
Author: User

Javascript object-oriented features and javascript object-oriented
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> untitled document </title>
</Head>
<Script type = "text/javascript">
Document. write ("js object-oriented: encapsulation ");
Function Person (name, sal ){
This. name = name; // public
Var sal = sal; // Private

This. showInfo = function () {// public
Document. write ("<br/>" + this. name + "--" + sal );
}
}
Var per = new Person ("tom", 100 );
Per. showInfo ();
Document. write ("<br/> js object-oriented: inheritance ");
// 1. extract common attributes and write a parent class
Function Stu (name, age ){
This. name = name;
This. age = age;
This. show = function (){
Document. write ("<br/>" + this. name + "--" + this. age );
}
}
Function MidStu (name, age ){
// Here it is equivalent to assigning the Stu Constructor (class) to our attribute this. stu
This. stu = Stu;
// This indicates the initialization of the MidStu, which is equivalent to the execution of the Stu (name, age). This statement must exist; otherwise, the integration effect cannot be achieved.
This. stu (name, age );
// You can write your own functions.
This. pay = function (feature ){
Document. write ("<br/> tuition:" + fee * 0.8 );
}
}
Var midstu = new MidStu ("", 15 );
Midstu. show ();
Midstu. pay (1, 100 );
Document. write ("<br/> js object-oriented: Heavy Load ");
// Js implements heavy load by judging the number of parameters
Function Person1 (){
This. test1 = function (){

If (arguments. length = 1 ){
This. show1 (arguments [0]);
} Else if (arguments. length = 2 ){
This. show2 (arguments [0], arguments [1]);
} Else if (arguments. length = 3 ){
This. show3 (arguments [0], arguments [1], arguments [2]);
}


}
This. show1 = function (){
Document. write ("show1 () called" + );
}


This. show2 = function (a, B ){
Document. write ("show2 () called" + "--" + a + "--" + B );
}


Function show3 (a, B, c ){
Document. write ("show3 () called ");
}
}
Var p1 = new Person1 ();
// Js does not support overloading.
// P1.test1 ("a", "B", "c ");
P1.test1 ("a", "B ");
P1.test1 ("");
Document. write ("<br/> js object-oriented: overwrite ");
Function Fu (){
This. fu = function (){
Document. write ("<br/> parent class method ");
}
}
Function Zi (){
This. stu = Fu;
This. stu ();
This. fu = function (){
Document. write ("<br/> subclass method ");
}
}
Var zi = new Zi ();
Zi. fu ();
Document. write ("<br/> js object-oriented: Polymorphism ");
// Master Class
Function Master (name ){
This. nam = name;
// Method [feed animals]
}
// Add member functions using the original method
Master. prototype. feed = function (animal, food ){

Document. write ("<br> to" + animal. name + "" + food. name );
}
Function Food (name ){
This. name = name;
}
// Fish
Function Fish (name ){
This. food = Food;
This. food (name );
}
// Bone
Function Bone (name ){
This. food = Food;
This. food (name );
}
// Peach
Function Peach (name ){
This. food = Food;
This. food (name );
}
// Animal
Function Animal (name ){
This. name = name;
}
// Cat
Function Cat (name ){
This. animal = Animal;
This. animal (name );
}
// Dog
Function Dog (name ){
This. animal = Animal;
This. animal (name );
}
// Monkey
Function Monkey (name ){
This. animal = Animal;
This. animal (name );
}
Var cat = new Cat ("Big cat ");
Var fish = new Fish ("yellow croaker ");
Var dog = new Dog ("Dahua dog ");
Var bone = new Bone ("pig bone ");
// Create a master
Var master = new Master ("Han shunping ");
Master. feed (dog, bone );
// Extension
Var monkey = new Monkey ("Golden monkey ");
Var peach = new Peach ("Xiantao ");
Master. feed (monkey, peach );
</Script>
<Body>
</Body>
</Html>

Copyright statement: original post of the blogger. For details, refer to the source. Http://blog.csdn.net/dzy21

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.