


Detailed explanation of the difference between new function(){} and function(){}()_js object-oriented
Scenario 1:
var yx01 = new function() {return "center of circle"};
alert(yx01);
When we run the scenario 1 code, "[object object]" will be returned and displayed. At this time, the The code is equivalent to:
function anonymous class(){
return "center of circle";
}
var yx01 = new anonymous class();
alert(yx01); us Make the following changes to the code of scenario one:
var yx01 = new function() {return new String("center of the circle")};
alert(yx01);
When we run it, we will find What is returned is the "center of the circle". Why is this?
As long as the constructor after the new expression returns a reference object (array, object, function, etc.), it will overwrite the anonymous object created by new. If it returns a primitive type (without return) In fact, it is return original type undefined), then return the anonymous object created by new
Because new String will construct an object, not a string literal, and if new String(x) takes parameters, then it will be alerted when it is will return x. So yx01 will return the new String("center of the circle") object, and alert yx01 will display the "center of the circle".
Scenario 2:
var yx02 = function() {return "center of circle"}();
alert(yx02); When we run scenario 2 code, the "center of circle" will be displayed. , at this time the code is equivalent to:
var anonymous function = function() {return "center of circle"};
yx02 = anonymous function();
alert(yx02); Obviously, yx02 returns the execution result value of the anonymous function, that is, yx02 is: "center of the circle".
Of course, the execution result of the anonymous function can also be an anonymous object. For specific common applications, please see "A Module Pattern of Javascript"

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

To deeply analyze the difference between the lenb function and the len function, specific code examples are required. In the Python programming language, strings are a common data type, and string-related operations and processing are often required. In the process of string processing, we often use the function of obtaining the length of a string. Python provides two built-in functions to obtain the length of a string, namely the lenb function and the len function. Although their function names are very similar, they actually have important differences in how they handle string lengths.

Differences: 1. Make can only be used to allocate and initialize data of types slice, map, and chan; while new can allocate any type of data. 2. New allocation returns a pointer, which is the type "*Type"; while make returns a reference, which is Type. 3. The space allocated by new will be cleared; after make allocates the space, it will be initialized.

1. Concept In the Java language, the "new" expression is responsible for creating an instance, in which the constructor is called to initialize the instance; the return value type of the constructor itself is void, not "the constructor returns the newly created Object reference", but the value of the new expression is a reference to the newly created object. 2. Purpose: Create an object of a new class. 3. Working mechanism: Allocate memory space for object members, and specify default values. Explicitly initialize member variables, perform construction method calculations, and return reference values. 4. Instance new operation often means opening up new memory in the memory. The memory space is allocated in the heap area in the memory. It is controlled by jvm and automatically manages the memory. Here we use the String class as an example. Pu

C language is a general programming language that is widely used in system software development, embedded systems, game development and other fields. In comparison, other programming languages such as Python, Java, JavaScript, etc. also have their own advantages in different fields. This article will compare and differ between C language and other programming languages, and give specific code examples to illustrate. First of all, C language is a procedural programming language that focuses on the control and optimization of the underlying computer. In contrast, Python is an object-oriented


How does the new operator in js work? Specific code examples are needed. The new operator in js is a keyword used to create objects. Its function is to create a new instance object based on the specified constructor and return a reference to the object. When using the new operator, the following steps are actually performed: create a new empty object; point the prototype of the empty object to the prototype object of the constructor; assign the scope of the constructor to the new object (so this points to new object); execute the code in the constructor and give the new object

Fujifilm has seen a lot of success in recent years, largely due to its film simulations and the popularity of its compact rangefinger-style cameras on social media. However, it doesn't seem to be resting on its laurels, according to Fujirumors. The u

Using clone() instead of new The most common way to create a new object instance in Java is to use the new keyword. JDK's support for new is very good. When using the new keyword to create lightweight objects, it is very fast. However, for heavyweight objects, the execution time of the constructor may be longer because the object may perform some complex and time-consuming operations in the constructor. As a result, the system cannot obtain a large number of instances in the short term. To solve this problem, you can use the Object.clone() method. The Object.clone() method can bypass the constructor and quickly copy an object instance. However, by default, the instance generated by the clone() method is only a shallow copy of the original object.
