Object-oriented JavaScript is a relatively popular concept in recent years. Due to my limited talent and limited knowledge, although I have done many web projects and read a lot of esoteric materials and tutorials on the Internet, I still have little understanding of their esoteric theories. I read it some time ago. After reading some books, I finally have my own understanding. Today I am going to pretend for a while. If you think it is very profound, please despise me directly. If you think it is wrong, please just criticize me.
First understand the following things in a general way.
Writing function fn(){} or var fn=function(){}, etc. in JS code, you can understand it as an object, of course, an array, etc.
Before understanding object-oriented, let’s first understand the following things.
1. Calling object methods
The function written in the outermost layer of js can also be understood as a method of the window object. The defined variable can also be called a property of the window object. For example:
var test=function(){ alert("123") } 当然上面的你也可以这样定义 function test(){ alert("123") }
As a method of the window object, we can call it like this
test()//Pop up the warning box 123 (because the window object is a top-level object, we can omit it)
window .test()//The warning box 123 pops up
window['test']()//The warning box 123 pops up. Test can be understood as a method value under the window array object.
Through the above examples, we have a general understanding of how to use and call object methods.
2, Private methods
Private methods are methods that can only be used within the scope of the object. This can be understood in terms of variable scope.
The functions in the above examples can be understood as private methods of the window object. Continue with the example below.
var pet=function(){ function showpet(){ alert("123") } showpet();//私有方法可以在函数作用域范围内使用。 var temp="私有变量只有在函数或者对象作用域范围内能访问" } showpet();//会出错 pet.showpet()//还是不能这样调用 var Penguin=new pet() //实例化一个pet对象 Penguin.showpet()//不好意思这样子还是不能让你调用。
What should I do if the method I want to define can be called outside the scope of the object? How can I use private methods? Let’s look at the next bit.
3. Static method
With the above questions, let’s continue the above example.
var pet=function(){ function showpet(){//私有方法 alert("123") } showpet();//私有方法可以在函数作用域范围内使用。 } pet.show=function(){//给pet对象添加一个静态方法。 alert("456") } pet.name="Penguin"//给pet对象添加一个静态属性。 pet.show()//弹出警告框456 alert(pet.name)//弹出警告框Penguin //继续思维碰撞 ===================== var Penguin=new pet() //实例化一个pet对象 Penguin.show()//不好意思,这个静态方法好像没有被实例继承。有这种思路值得表扬啊,加油!
The above example shows you what a static method is. Of course, you may not understand it. In fact, I don’t understand it either, because I am also a novice, but as long as you read it, you will know how to write a static method for an object. Method, just how to call the static method. Maybe one day, you will suddenly understand and come back to teach me. With the above questions, let's take a look at the methods that can be called by instantiated objects.
4. Public methods
Public methods are usually implemented by modifying the prototype of the constructor. After modifying the prototype of an object, all instances of the object will inherit the modification of the prototype (this sentence Extremely pretentious, please ignore it if you feel vague).
Modify the object prototype method and continue the above example.
pet.prototype.setname=function(str){//通过修改原型添加一个公有方法,用于添加修改实例对象的name name=str; }
Look at the example:
var pet=function(){ function showname(){//私有方法 alert(this.name) } this.show=function(){ //如果这里不理解,请注意这个方法下面就要介绍了。 showname(); } } pet.prototype.setname=function(str){ name=str; } var Penguin=new pet() Penguin.setname("Penguin");//添加实例的name值为Penguin Penguin.show(); //弹出Penguin Penguin.setname("wind");//添加实例的name值为wind Penguin.show(); //弹出wind
Run the code and have fun.
<script> var pet=function(){ name:"123", function showname(){//私有方法 alert(this.name) } this.show=function(){ showname(); } } pet.prototype.setname=function(str){ name=str; } var Penguin=new pet() Penguin.setname("Penguin"); Penguin.show(); Penguin.setname("wind"); Penguin.show(); </script>
5. Privileged method (object or function external interface)
In fact, we have already used this method in the above example. This method can be called by instantiated object inheritance. By defining the method via the thsi keyword inside the constructor. Privileged methods can be publicly accessed outside the constructor (limited to instantiated objects), and can also access private members and methods, so they are most suitable as interfaces for objects or constructors. Through privileged functions we can control public Method access to private methods, which has many applications in JS framework extensions.
Readers can think that the above is a paragraph of P. Let’s take a look at how to define a privileged method, how to reference a privileged method, and continue to call the above examples to see.
var pet=function(){ function showname(){//私有方法 alert(this.name) } this.show=function(){//通过使用this关键字定义一个特权方法。 showname(); //在特权方法中访问私有方法; } } pet.prototype.setname=function(str){ name=str; } var Penguin=new pet();//实例化一个pet对象 Penguin.setname("Penguin");//调用公有方法修改 Penguin.show(); //调用特权方法访问私有方法,弹出name
First create a privileged method by using this.fn=function(){} in the constructor. Access private methods in privileged functions;
The instantiated object can use some private methods by accessing privileged functions. The method of accessing privileged functions is the same as accessing public functions.
The first part is here for the time being. The next part will use an example to explain how to install B in the object-oriented way.
The above is the entire content of this article. I hope it will be helpful to everyone. Friends who are interested can read "Javascript Object-Oriented--Encapsulation". Thank you for your support to the PHP Chinese website!

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use