Home Web Front-end JS Tutorial Explanation of the difference between determining whether a function is called new or () in JavaScript_javascript skills

Explanation of the difference between determining whether a function is called new or () in JavaScript_javascript skills

May 16, 2016 pm 06:08 PM
new Judgment function

Method 1

Copy code The code is as follows:

function Person(n,a){
this.name = n;
this.age = a;
if(this instanceof Person){
alert('new call');
}else{
alert('function Call ');
}
}
var p = new Person('jack',30); // --> new call
Person(); // --> function Call

Method 2
Copy code The code is as follows:

function Person(n,a){
this.name = n;
this.age = a;
if(this instanceof arguments.callee){
alert('new call');
}else{
alert('function call');
}
}
var p = new Person('jack',30); // --> new call
Person(); // --> Function call

Method 3
Copy code The code is as follows:

function Person(n,a){
this.name = n;
this.age = a;
if(this.constructor === arguments .callee){
alert('new call');
}else{
alert('function call');
}
}
var p = new Person(' jack',30); // --> new call
Person(); // --> function call

seems perfect, but when the function/class is used as There is a problem when calling the method of the own instance object
Copy the code The code is as follows:

function Person(n,a){
this.name = n;
this.age = a;
if(this.constructor === arguments.callee){
alert('new call' );
}else{
alert('function call');
}
}
var p = new Person('jack',30); // new object first
p.fn = Person; // Assign the function/class Person to the fn attribute of the own object p
p.fn(); // When calling this sentence, it prompts "This is a new call", which is obviously wrong

Is there a better way?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between make and new in go language What is the difference between make and new in go language Jan 09, 2023 am 11:44 AM

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 use the new keyword in java How to use the new keyword in java May 03, 2023 pm 10:16 PM

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 work in js? How does the new operator work in js? Feb 19, 2024 am 11:17 AM

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

Sharing of commonly used excel judgment functions Sharing of commonly used excel judgment functions Mar 19, 2024 pm 09:30 PM

Excel tables exist in all walks of life. Whether you are a workplace worker, a teacher, or even a staff member of a state agency, you must frequently use Excel tools. Mastering Excel operating skills can add luster to your work and help you when necessary. get promoted. The editor of this article mainly shares with you commonly used Excel judgment functions. 1. Commonly used excel judgment functions 1. IF function: returns different values ​​​​according to conditional judgment; syntax =IF (conditional judgment, the result is a true return value, the result is a false return value) 2. OR function: perform multiple logical conditions Judgment; syntax = OR (condition 1 = criterion 1, condition 2 = criterion 2,...condition N = criterion N) 3. AND function: multiple conditions are established at the same time;

New Fujifilm fixed-lens GFX camera to debut new medium format sensor, could kick off all-new series New Fujifilm fixed-lens GFX camera to debut new medium format sensor, could kick off all-new series Sep 27, 2024 am 06:03 AM

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

How to use clone() method in Java instead of new keyword? How to use clone() method in Java instead of new keyword? Apr 22, 2023 pm 07:55 PM

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.

How to use new instantiation in java How to use new instantiation in java May 16, 2023 pm 07:04 PM

1. The concept is "create a Java object" ----- allocate memory and return a reference to that memory. 2. Notes (1) The Java keyword new is an operator. It has the same or similar precedence as +, -, *, / and other operators. (2) Creating a Java object requires three steps: declaring reference variables, instantiating, and initializing the object instance. (3) Before instantiation, the parameterless constructor of the parent class will be called by default, that is, an object of the parent class must be created. 3. Two instantiation methods (1) Object name = new class name (parameter 1, parameter 2... Parameter n); object name. method (); (2) new class name (parameter 1, parameter 2... parameter n). method; 4. Example uses a simple code to illustrate how to make the object real

See all articles