Javascript is based on three main characteristics of objects (encapsulation, inheritance, polymorphism) and javascript polymorphism.
Javascript is based on the three features of objects and the three features of C ++ and Java object-oriented, which are encapsulation, inheritance, and polymorphism ). However, the implementation methods are different, and their basic concepts are similar. In fact, apart from the three main features, there is also a common feature called abstract, which is why we sometimes see four features of object-oriented in some books.
I. Encapsulation
Encapsulation encapsulates the abstracted data and the operations on the data, and the data is protected internally. Other parts of the program are only authorized operations (member methods ), to operate the data.
Case:
<Html>
PS: JS encapsulation only has two states: public and private.
Differences between adding member methods by using constructors and adding member methods by using primitive methods
1. All objects are shared by the functions allocated by the original method.
2. Attributes allocated through the original method are independent. (If you do not modify attributes, they are shared)
3. We recommend that you use the original method to add a function if you want all objects to use the same function, which saves memory.
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 learned how to add methods to all objects through prototype. However, this method cannot be used to add private variables and methods of the classification class.
Ii. Inheritance
Inheritance can solve code reuse and make programming closer to human thinking. When multiple classes have the same attributes (variables) and methods, you can abstract the parent class from these classes and define these identical attributes and methods in the parent class, all subclasses do not need to redefine these attributes and methods. They only need to inherit the attributes and methods in the parent class.
How to Implement inheritance in JS
1. Object impersonating
Case:
<Html>
2. call or apply
Case:
<Html>
Summary:
1. Javascript objects can be impersonated by objects to achieve multi-Inheritance
2. The Object class is the base class of all Js classes.
Iii. Polymorphism
JS function overload
This is the basis of polymorphism. As mentioned earlier in Javascript, Javascript functions do not support polymorphism, but in fact Javascript Functions are stateless and support a list of parameters of any length and type. If multiple functions with the same name are defined at the same time, the last function prevails.
Case:
<Html>
1. Basic concepts of Polymorphism
Polymorphism refers to multiple States of a reference (type) under different circumstances. It can also be understood that polymorphism refers to calling methods implemented in different subclasses by pointing to the reference of the parent class.
Case:
<Script type = "text/javascript"> // Master class function Master (name) {this. nam = name; // method [feed animals]} // Add the member function Master to the original method. prototype. feed = function (animal, food) {window. alert ("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);} 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 ("cat"); var fish = new Fish ("fish"); 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 is conducive to code maintenance and expansion. When we need to use objects in the same class tree, we only need to input different parameters, instead of new objects.
These are the three main features of Javascript Based on objects, and hope to help you learn them.
Articles you may be interested in:
- Javascript object-oriented feature reference
- Javascript object-oriented features
- Use javascript object-oriented features to limit the trial period
- Javascript object-oriented code example