JavaScript is based on the three main characteristics of the object (encapsulation, inheritance, polymorphism) _javascript skills

Source: Internet
Author: User
Tags abstract inheritance

JavaScript is based on the three main features of the object and the three main features of C++,java object-oriented, both encapsulation (encapsulation), Inheritance (inheritance), and polymorphism (polymorphism). But the basic concept is the same as the way it is implemented. In fact, in addition to the three major features, there is a common feature called abstract, which is why we sometimes see the four characteristics of the object in some books.
first, the encapsulation of
encapsulation is the encapsulation of the abstracted data and the operation of the data, the data is protected internally, and other parts of the program can operate on the data only through authorized operations (member methods).
Case:

 
 

The PS:JS package has only two states, one is public and one is private.

The difference between adding a member method through a constructor and adding a member method by using the stereotype method
1. The functions assigned by the prototype method are shared by all objects.
2. The attributes assigned through the archetypal method are independent. (If you do not modify attributes, they are shared)
3, it is recommended that if we want all objects to use the same function, it is best to use the prototype method to add functions, so that more memory savings.

Case:

function person () {
  this.name= "ZS";
  var age=20;
  This.abc=function () {
    window.alert ("abc");
  }
  function ABC2 () {
    window.alert ("abc");
  }
Person.prototype.fun1=function () {
  window.alert (this.name);//ok
  //window.alert (age);//no OK
  // ABC2 ();//no OK
  this.abc ();//ok
}
var p1=new person ();
P1.fun1 ();

Special emphasis: We have previously learned to add methods to all objects through prototype, but this approach does not access the class's private variables and methods.

Second, the succession of
inheritance solves code reuse and allows programming to be closer to human thinking. When multiple classes have the same properties (variables) and methods, you can abstract the parent class from these classes, define the same properties and methods in the parent class, and all subclasses do not need to redefine these properties and methods, only by inheriting the properties and methods in the parent class.
How to implement inheritance in JS
1. Object posing as
Case:

 

2, implementing by Call or Apply
case:

 

Summary:
1, JS objects can be spoofed through the object, to achieve multiple inheritance
2, the object class is all JS class base class

Three, polymorphism
function overload of JS
This is the basis of polymorphism, in the previous JavaScript primer already said, JS function does not support polymorphism, but in fact the JS function is not state, support arbitrary length, type of parameter list. If multiple functions with the same name are defined at the same time, the last function is the one.
Case:

 

1, polymorphic basic concepts
Polymorphism refers to the multiple states of a reference (type) in different situations. It can also be understood that polymorphism refers to a method that is implemented in different subclasses by referring to a reference to the parent class.
Case:

<script type= "Text/javascript" >//Master class function Master (name) {this.nam=name; methods [Feed animals]}//Prototyping method Add member function master.prototype.feed=function (animal,food) {Window.alert ("give" +animal.name+ "feed" + 
  Food.name); 
  function Food (name) {this.name=name; 
    //Fish function fishes (name) {This.food=food; 
  This.food (name); 
    }//Bone function bone (name) {This.food=food; 
  This.food (name); 
    function Peach (name) {This.food=food; 
  This.food (name); 
  }//Animal class 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 ("Cat"); 
 
  var fish=new fish ("fishes"); 
  var dog=new dog ("Dog"); 
 
  var bone=new bone ("bone"); 
  var monkey=new monkey ("monkey"); var Peach=neW Peach ("Peach"); 
  Create a master var master=new master ("Zs"); 
  Master.feed (Dog,bone); 
  Master.feed (Cat,fish); 
Master.feed (Monkey,peach); 
 </script>

Polymorphism facilitates the maintenance and extension of code, and when we need to use objects on the same tree, we just need to pass in a different parameter instead of a new object.

The above is JavaScript based on the three main characteristics of the object, I hope to help you learn.

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.