Home >Web Front-end >JS Tutorial >Javascript is based on the three major characteristics of objects (encapsulation, inheritance, polymorphism)_javascript skills
The three major object-based characteristics of Javascript are the same as the three major object-oriented characteristics of C++ and Java, which are encapsulation, inheritance and polymorphism. It's just that the implementation methods are different, but the basic concept is almost the same. In fact, in addition to the three major characteristics, there is another common characteristic called abstract, which is why we sometimes see the four major characteristics of object-oriented in some books.
1. Encapsulation
Encapsulation is to encapsulate abstracted data and operations on the data. The data is protected internally. Other parts of the program can only operate on the data through authorized operations (member methods).
Case:
<html> <head> <script type="text/javascript"> function Person(name, agei, sal){ // 公开 this.name = name; // 私有 var age = agei; var salary = sal; } var p1 = new Person('zs', 20, 10000); window.alert(p1.name + p1.age); </script> </head> <body> </body> </html>
PS: JS encapsulation has only two states, one is public and the other is private.
The difference between adding member methods through constructors and adding member methods through prototype methods
1. Functions allocated through the prototype method are shared by all objects.
2. The attributes assigned through the prototype method are independent. (If you do not modify the 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 the 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 learned earlier to add methods to all objects through prototype, but this method cannot access private variables and methods of the class.
2. 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 same attributes and methods in the parent class. All subclasses do not need to redefine these attributes and methods. Just inherit the properties and methods from the parent class.
How to implement inheritance in JS
1. Object impersonation
Case:
<html> <head> <script type="text/javascript"> function Stu(name, age){ this.name = name; this.age = age; this.show = function(){ window.alert(this.name + " " + this.age); } } function MidStu(name, age) { this.stu = Stu; // 通过对象冒充来实现继承的 // 对象冒充的意思就是获取那个类的所有成员,因为js是谁调用那个成员就是谁的,这样MidStu就有了Stu的成员了 this.stu(name, age); this.payFee = function(){ window.alert("缴费" + money * 0.8); } } function Pupil(name, age) { this.stu = Stu; // 通过对象冒充来实现继承的 this.stu(name, age); this.payFee = function(){ window.alert("缴费" + money * 0.5); } } var midStu = new MidStu("zs", 13); midStu.show(); var pupil = new Pupil("ls", 10); pupil.show(); </script> </head> <body> </body> </html>
2. Through call or apply
Case:
<html> <head> <script type="text/javascript"> //1. 把子类中共有的属性和方法抽取出,定义一个父类Stu function Stu(name,age){ // window.alert("确实被调用."); this.name=name; this.age=age; this.show=function(){ window.alert(this.name+"年龄是="+this.age); } } //2.通过对象冒充来继承父类的属性的方法 function MidStu(name,age){ //这里这样理解: 通过call修改了Stu构造函数的this指向, //让它指向了调用者本身. Stu.call(this,name,age); //如果用apply实现,则可以 //Stu.apply(this,[name,age]); //说明传入的参数是 数组方式 //可以写MidStu自己的方法. this.pay=function(fee){ window.alert("你的学费是"+fee*0.8); } } function Pupil(name,age){ Stu.call(this,name,age);//当我们创建Pupil对象实例,Stu的构造函数会被执行,当执行后,我们Pupil对象就获取从 Stu封装的属性和方法 //可以写Pupil自己的方法. this.pay=function(fee){ window.alert("你的学费是"+fee*0.5); } } //测试 var midstu=new MidStu("zs",15); var pupil=new Pupil("ls",12); midstu.show(); midstu.pay(100); pupil.show(); pupil.pay(100); </script> </html>
Summary:
1. JS objects can be impersonated through objects to achieve multiple inheritance
2. The Object class is the base class of all Js classes
3. Polymorphism
JS function overloading
This is the basis of polymorphism. As mentioned in the previous introduction to Javascript, JS functions do not support polymorphism, but in fact JS functions are stateless and support parameter lists of any length and type. If multiple functions with the same name are defined at the same time, the last function shall prevail.
Case:
<html> <head> <script type="text/javascript"> //*****************说明js不支持重载***** /*function Person(){ this.test1=function (a,b){ window.alert('function (a,b)'); } this.test1=function (a){ window.alert('function (a)'); } } var p1=new Person(); //js中不支持重载. //但是这不会报错,js会默认是最后同名一个函数,可以看做是后面的把前面的覆盖了。 p1.test1("a","b"); p1.test1("a");*/ //js怎么实现重载.通过判断参数的个数来实现重载 function Person(){ 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(a){ window.alert("show1()被调用"+a); } this.show2=function(a,b){ window.alert("show2()被调用"+"--"+a+"--"+b); } function show3(a,b,c){ window.alert("show3()被调用"); } } var p1=new Person(); //js中不支持重载. p1.test1("a","b"); p1.test1("a"); </script> </html>
1. Basic concepts of polymorphism
Polymorphism refers to the multiple states of a reference (type) under different circumstances. It can also be understood as: Polymorphism refers to calling methods implemented in different subclasses through references pointing to the parent class.
Case:
<script type="text/javascript"> // Master类 function Master(name){ this.nam=name; //方法[给动物喂食物] } //原型法添加成员函数 Master.prototype.feed=function (animal,food){ window.alert("给"+animal.name+" 喂"+ food.name); } function Food(name){ this.name=name; } //鱼类 function Fish(name){ this.food=Food; this.food(name); } //骨头 function Bone(name){ this.food=Food; this.food(name); } function Peach(name){ this.food=Food; this.food(name); } //动物类 function Animal(name){ this.name=name; } //猫猫 function Cat(name){ this.animal=Animal; this.animal(name); } //狗狗 function Dog(name){ this.animal=Animal; this.animal(name); } //猴子 function Monkey(name){ this.animal=Animal; this.animal(name); } var cat=new Cat("猫"); var fish=new Fish("鱼"); var dog=new Dog("狗"); var bone=new Bone("骨头"); var monkey=new Monkey("猴"); var peach=new Peach("桃"); //创建一个主人 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 on the same type of tree, we only need to pass in different parameters without the need to create a new object.
The above are the three major characteristics of Javascript based on objects. I hope it will be helpful to everyone's learning.