Use hidden new to create objects_jquery
Many times we write classes like this and then use new to create objects.
function Person(name,age){
this .name=name;
this.age=age;
}
Person.prototype={
setName : function(n){this.name=n;},
getName : function (){return this.name;}
}
var p = new Person('jack',25);
Change to this
function Person(name,age){
//The condition is changed to (this== window) or (this==self) or (this.constructor!=Object)
if(!this.setName){
return new Person(name,age);
}
this. name=name;
this.age=age;
}
Person.prototype={
setName : function(n){this.name=n;},
getName : function( ){return this.name;}
}
var p = Person('jack',25);
Note that this class has the following additions compared to the top way of writing classes.
if(!this.setName){
return new Person(name,age);
}
Okay, the way to create an instance (object) of the class has also become as follows
var p = Person('jack',25);
This way of creation ( Function call) is less "new_", new and spaces than the above one. It is actually new inside the class. This method can reduce 4 bytes each time an object is created.
If the if judgment condition inside the class is replaced by a non-prototype attribute, such as this.name. The program will prompt an error: too much recursion
function Person(name ,age){
if(!this.name){
return new Person(name,age);
}
this.name=name;
this.age=age;
}
Person.prototype={
setName : function(n){this.name=n;},
getName : function(){return this.name;}
}
var p = Person('jack',25);

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

Solution: 1. Check spelling and path; 2. Add reference to component; 3. Check registry; 4. Run as administrator; 5. Update or repair Office; 6. Check security software; 7. Use other versions Components; 8. View error messages; 9. Find other solutions. Detailed introduction: 1. Check spelling and path: Make sure there are no spelling errors in the name and path of the object, and the file does exist in the specified path; 2. Add references to components, etc.

The steps to create an object through the Java reflection mechanism are as follows: Load the target class: Use the Class.forName() method. Get the constructor: use the getDeclaredConstructor() method. Create an object: Use the newInstance() method to pass parameters.

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.

How to create and initialize classes and objects in Go language. Although Go language does not have the concept of classes in traditional object-oriented languages, we can achieve similar functions through structures and methods. In this article, we will learn how to create and initialize classes and objects in Go language. 1. Define the structure of the class In the Go language, we can use the structure to define the attributes and methods of the class. A structure is a custom composite type that can contain multiple fields of different types. For example, if we want to implement a rectangle class, we can define it as

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


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
