


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!)

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



How to use Go language to implement object-oriented event-driven programming Introduction: The object-oriented programming paradigm is widely used in software development, and event-driven programming is a common programming model that realizes the program flow through the triggering and processing of events. control. This article will introduce how to implement object-oriented event-driven programming using Go language and provide code examples. 1. The concept of event-driven programming Event-driven programming is a programming model based on events and messages, which transfers the flow control of the program to the triggering and processing of events. in event driven

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming In object-oriented programming, design pattern is a commonly used software design method, which can improve the readability, maintainability and scalability of the code. Flyweight pattern is one of the design patterns that reduces memory overhead by sharing objects. This article will explore how to use flyweight mode in PHP to improve program performance. What is flyweight mode? Flyweight pattern is a structural design pattern whose purpose is to share the same object between different objects.

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

By mastering tracking object status, setting breakpoints, tracking exceptions and utilizing the xdebug extension, you can effectively debug PHP object-oriented programming code. 1. Track object status: Use var_dump() and print_r() to view object attributes and method values. 2. Set a breakpoint: Set a breakpoint in the development environment, and the debugger will pause when execution reaches the breakpoint, making it easier to check the object status. 3. Trace exceptions: Use try-catch blocks and getTraceAsString() to get the stack trace and message when the exception occurs. 4. Use the debugger: The xdebug_var_dump() function can inspect the contents of variables during code execution.
