JS object-oriented programming object usage analysis_javascript skills
Because everyone always uses process-oriented programming ideas to write JS code, and because the Internet is full of too many small "clever" JS code snippets, many of which are random and very irregular, this has caused everyone to The "misunderstanding" of JS is that it is just a small auxiliary tool and is not suitable for the development of large things. However, since the rise of AJAX, writing a large amount of JS code requires people to be able to develop object-oriented development just like writing JAVA-like code.
So below, I will combine my own experience and what I have learned with you to learn how to use object-oriented programming in JS. In fact, it is not difficult to use JS for object-oriented development, because each function in JS is an object, such as the following function:
function HelloWorld()
{
alert('hello world!');
}
Then we can use it as an object when using it, such as using the following test function:
function _test()
{
var obj = new HelloWorld();
}
Then call the _test method Then hello world will pop up! prompt box, that is, the HelloWorld() object (function) is called. The HelloWorld object here does not have any properties or methods. It has only one construction method HelloWorld(). We can think of it as a class in JAVA without any properties and methods. When using new to create an object, it is called Its construction method. This is also our simplest object. Of course, an object must be assigned attributes and methods. In JS, we use the prototype keyword to assign values. For example, I want to add a sayHello method and a name attribute to the HelloWorld object. , then you can add it like this:
HelloWorld.prototype = {
name : 'JavaScript',
sayHello : function() {
alert(this.name);
}
}
Then it can be HelloWorld Added a name attribute and sayHello method, let's change the _test method as follows:
function _test()
{
var obj = new HelloWorld();
obj.sayHello();
}
Then call After the _test method, hello wordl! and JavaScript will be printed successively (one is the alert in the construction method, and the other is the alert in the sayHello method). Note that the alert in the sayHello method refers to the this keyword, which represents the HelloWorld object and points to this object by default, just like the this keyword in JAVA.
To add instance methods and properties to an object, we can use the above method, that is, use the prototype keyword to assign values. The format is as follows:
Object name.prototype = {
Properties One: attribute value,
Attribute two: attribute value,
Method one: function (parameter list) {
Method body;
},
Method two: function (parameter list) {
Method body;
}
}
You can define multiple properties and methods for an object as above, so that after new an object, you can use the instance name.property or Method to get properties or execute methods.
In the above method, you may not know that no object's attributes can be directly accessed. For example, to access the name attribute of the HelloWorld object, you can use obj.name to obtain it directly. This is like the public attributes in our JAVA, and we can also directly assign values to the name attribute. So now there is a question, how do we assign a private member variable to an object? Then we may have to change the way of declaring the HelloWorld class. Instead of using prototype to declare the attributes and methods of the class, we directly use inline functions and attributes to declare. The modified HelloWorld is as follows, we named it HelloWorld2:
function HelloWorld2()
{
var privateProp = ' hello world 2!';
this.method = function() {
alert(privateProp);
}
}
Have you seen the class declaration method of HelloWorld2? The function nested declaration is made directly inside the function, and we also set a local variable privateProp, which is our private member variable. This variable can only be accessed by functions inside HelloWorld2. External access is not allowed, so we You can cleverly set private variables of a class by using the scope of the variable. Our application is as follows:
function _test2()
{
var obj2 = new HelloWorld2();
obj2.method(); // Calling this method will print 'hello world 2!
alert(obj2.privateProp); // Will print undefined
}
The above are all about how to define a class, and how to define attributes and methods for a class. Since the definition is clear and clear using the prototype method, this method is generally used to define the class. Definition, and similar class declaration methods are now used in many AJAX frameworks. Moreover, the private member variables of the class can only be accessed by functions in the construction method of the class. In this way, the methods declared by the prototype of the class cannot access the private member variables, and the readability is not as good as the prototype method.
Okay, the above is all about defining instance methods and attributes of a class. In JAVA, classes are divided into instance methods and properties and class methods and properties. The so-called class attributes and methods mean that all instances of the class only maintain a copy of the class attributes and class methods, instead of maintaining a set for each instance. This is different from instance attributes and instance methods. So how to define static class methods and class attributes for a class in JS? We can directly add static attributes and static methods to the class. For example, add an age static attribute and a hello static method to the HelloWorld class, then the declaration is as follows:
HelloWorld.age = 22;
HelloWorld.hello = function() {
alert(HelloWorld.age);
}
Then the static attribute age and the static method hello are declared for the class HelloWorld. When using it, you can directly use the class name to access, but you cannot use the instance to access. This is consistent with JAVA. The test is as follows:
function _test()
{
var obj = new HelloWorld();
obj.sayHello(); / / Correct, instance method, can be accessed through the instance
HelloWorld.hello(); // Correct, static method, directly accessed through the class name
obj.hello(); // Error, cannot be accessed through the instance static method. A JS error will be reported!
}
Through the above explanation, I believe that everyone has a certain understanding of object-oriented programming with JS, and they must be ready to start. Haha, you might as well give it a try~~ (Note : All the above codes passed the test!)

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.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

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.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool