


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.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version