JavaScript object-oriented feature code examples_javascript skills
1. Use of basic classes
Method 1:
function sth(a) // Constructor
{
this.a = a;
this.fun = output; // Member function
}
function output(a, b, c)
{
document.write(this.a);
}
//Call
var s = new sth(250);
s.fun(1, 2, 3);
ouput(1, 2, 3); //If output is in sth It was wrong before
Method 2:
{
this.a = a;
this.output = function()
{
document.write(this.a);
}
}
var s = new sth(2);
s.output(); // Output 2
2. Inheritance
Method 1:
{
this.x = x;
}
function B(x, y)
{
// Method 1
/*
this.construct = A;
this.construct(x);
delete this .construct;
*/
// Method 2
//A.call(this, x);
// Method 3
A.apply(this , new Array(x)); // You can also A.apply(this, arguments), but the order of arguments must be correct
this.y = y;
this.print = function()
{
document.write("x = ", x,
", y = ", y);
}
}
var b = new B(1, 2);
b.print();
alert(B instanceof A); // Output false
Advantages: Multiple inheritance can be achieved (just make multiple calls)
Disadvantages:
· Must be used as a constructor
· Using the instanceof operator to operate this type of inheritance results in false
Method 2:
{
}
A.prototype.x = 1;
function B()
{
}
B.prototype = new A(); // Cannot take parameters!
B.prototype.y = 2;
B.prototype.print = function()
{
document.write(this.x, ", ", this.y, "
}
var b = new B();
b.print();
document.write(b instanceof A); // Output true
Disadvantages:
· Cannot implement multiple inheritance
· The constructor does not take parameters
Tips
Usually use mixed mode, both together
function A(x)
{
this.x = x;
}
A.prototype.printx = function() // Write into class A this.printx = function.... It’s also possible, the same below
{
document.write(this.x, "
");
}
function B(x, y)
{
A.call(this, x);
this.y = y;
}
B.prototype = new A(); // Cannot take parameters!
B.prototype.printxy = function()
{
document.write(this.x, ", ", this.y, "
");
}
var b = new B(1, 2);
b.printx(); // Output 1
b.printxy(); // Output 1, 2
document.write(b instanceof A); // Output true
3. Use of similar static member functions
{
this.a = a;
}
sth.fun = function(s)
{
document.write(s.a );
}
var s = new sth(2);
sth.fun(s); // Output 2
4. Release of objects
obj = null; // Dereference will automatically perform garbage collection; if you need to release this object at all, assign all its references to null
5. Function object
v(1, 2); // Will Will output 3
6. Callback function
{
func(arg);
}
function fun(arg)
{
document.write(arg);
}
//callback(func, "sb"); // This approach does not work
var func = new Function("arg", "fun(arg);");
// Of course, you can also replace func(arg) with specific execution code,
// But it is best to do this if the function code is huge
callback(func, "sb");
7. Overloading of functions
{
switch (arguments.length)
{
case 1:
document.write(arguments[0]);
break;
case 2 :
document.write(arguments[0] arguments[1]);
break;
default:
document.write("ERROR!");
break;
}
}
fun(1);
fun(1, 2);
8. Use function closures to implement functions with "static variables"
{
var v = 1;
function fun2()
{
v;
document.write(v);
document.write("
");
return v;
}
return fun2;
}
var func = fun() ;
func(); // Output 2
func(); // Output 3
func(); // Output 4

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

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

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.

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

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

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.

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.

JavaScript and WebSocket: Building an efficient real-time search engine Introduction: With the development of the Internet, users have higher and higher requirements for real-time search engines. When searching with traditional search engines, users need to click the search button to get results. This method cannot meet users' needs for real-time search results. Therefore, using JavaScript and WebSocket technology to implement real-time search engines has become a hot topic. This article will introduce in detail the use of JavaScript
