


JavaScript object-oriented methods and differences_js object-oriented
The first one: use this keyword
function Class1()
{
this.onclick = function(e)
{
for (var i=0; i Add properties and methods, similar to most OOP languages, and can even be added at runtime.
Second: Use prototype keyword
function clickFunc(e)
{
for (var i=0; i {
var a = new Date();
}
}
function Class2()
{
}
Class2.prototype.onclick = clickFunc;
This usage is not as flexible as the first one. However, before an object new comes out, you can also add the properties and methods of an object at any time.
But they are not equal. Relatively speaking, I prefer the first method because the first method is relatively concentrated and easier to read the code. But when running, their running efficiency is quite different. Let’s take a look at the test code below:
var total = new Array();
function Test1()
{
var a = new Date();
for (var i=0; i {
var c = new Class1();
//total.push(c);
}
var b = new Date();
alert(b.getTime()-a.getTime());
}
function Test2()
{
var a = new Date ();
for (var i=0; i {
var c = new Class2();
//total.push(c);
}
var b = new Date();
alert(b.getTime()-a.getTime());
}
The first step is to test the execution time: find Test1 () takes 142ms, while Test2() only takes 50ms. In terms of time efficiency, the prototype method is more efficient than this.
The second step is to test the memory usage. Remove the comments from the line total.push(c);. The reason why we need to add them to the array is to prevent there are many objects when creating them. , objects that are not referenced are GCed. It turns out that the gap is not that big. The first method takes up 20 to 30 M of memory, while the second method only requires more than 100 K.
Cause inference:
When processing these two codes, the first one, the JS parser, creates a separate method for each object, which increases the memory overhead and creates method, the running time is increased. Second, the JS parser, like most OOP compilers, stores the object's data segment and method segment separately. For the object's private data, there is one copy for each object, and these methods are Put it in the public method section, so it can reduce running time and memory overhead.

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.

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.

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

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
