Home Web Front-end JS Tutorial js pointing problem

js pointing problem

Dec 08, 2017 pm 03:00 PM
javascript oriented question

  1. This points to:
    json and the prototype chain are the same.
    I have read many articles that say it is more complicated.
    This points to the calling object.
    Post the code directly.


var x = {
         test:function(){
                          console.log(this);
                        };
              };
x.test()//x{...};
var n = x.test();
n();//Window
Copy after login
Copy after login

The first time is x call, so console.log is x, and the second time is equivalent to window. n(), which is called window, so the window is displayed.
Maybe my understanding is shallow. I think this is the object in front of the '.' of the function that contains 'this'. As for apply and call, there will be some changes, I will briefly mention them below.
The difference between apply and call is that apply is (object, [parameter set]) and call is (object, parameter, parameter, parameter, parameter, parameter...). I don’t know the rest yet. Post a code first.

    function ed(){
    this.age = ed;
    }; 
    function ing(){
    this.age = 2;
    this.sex = 0;
    this.showAge = function(){
        console.log(this.age);
        console.log(this.sex);
        }
    };
    var edObj = new ed();
    var ingObj = new ing();
    ingObj.showAge.apply(edObj);//2,Undefined
Copy after login
Copy after login

To put it bluntly, it is like a programmer changing computers for development. Except for the logic of processing data in his own mind, other environment variables must be used by others. The method in front of apply is the programmer's thinking, and the method in () is the new computer. As for the following parameters... they are the parameters required by the method. You can pass this by yourself.

2. Arrow function pointing: The arrow function does not have a name, so it cannot be called by name, so this always points to Window.

3. Variable pointing: I think this is involved Problem with memory pointers. But it is easy to understand, that is, constants occupy memory, and variables are added up. It's like 2+ children playing a game. As long as you don't change places or play with other people, what's yours is mine, and what's mine is yours. This memory is like a children's play field, and the toys owned by the children are their attributes (these children are relatively generous).
Let’s give three examples:

var xArr = [];
var xJson = {};
(()=>{
      let yArr = xArr,
         yJson = xJson;
       yArr.push(1);
       yJson.age = 1;
      })();//这里说明即便是块级变量也是可以一起参与玩耍的,屋里玩耍的孩子玩具一样可以被其他小孩在屋外展示。
console.log(xArr);//[1]; 
console.log(xJson);//{age: 1}
Copy after login
Copy after login

Because Y has never found anyone else to play with (see example 3 for how to find others to play with), so y’s toys are x’s toys.

var x = 0,
    a = 2.
    b = 3,
    y = x;
    console.log(y);//0
    y = a+b;
    console.log(x);//0
    console.log(y);//5
Copy after login
Copy after login

Because Y changed the place to play (opened a memory to point to), so x cannot get Y's toys.

var x = {},
    a = {},
    y = x,
    z = y,
    y = a;
    y.age = 1;
    console.log(x);//{}
    console.log(y);//{age:1}
    console.log(z);//{}
    console.log(a);//{age:1}
    z.age = 2;
    console.log(x);//{age:2}
Copy after login
Copy after login

This may be a story of meeting passers-by...


1.This points to:
json and the prototype chain are the same of.
I have read many articles that say it is more complicated.
This points to the calling object.
Post the code directly.

var x = {
         test:function(){
                          console.log(this);
                        };
              };
x.test()//x{...};
var n = x.test();
n();//Window
Copy after login
Copy after login

The first time is x call, so console.log is x. The second time is equivalent to window.n(), which is window call, so window is displayed.
Maybe my understanding is shallow. I think this is the object in front of the '.' of the function that contains 'this'. As for apply and call, there will be some changes, I will briefly mention them below.
The difference between apply and call is that apply is (object, [parameter set]), call is (object, parameter, parameter, parameter, parameter, parameter...), and I don’t know the rest yet. Post a code first.

    function ed(){
    this.age = ed;
    }; 
    function ing(){
    this.age = 2;
    this.sex = 0;
    this.showAge = function(){
        console.log(this.age);
        console.log(this.sex);
        }
    };
    var edObj = new ed();
    var ingObj = new ing();
    ingObj.showAge.apply(edObj);//2,Undefined
Copy after login
Copy after login

To put it bluntly, it is like a programmer changing computers for development. Except for the logic of processing data in his own mind, other environment variables must be used by others. The method in front of apply is the programmer's thinking, and the method in () is the new computer. As for the following parameters... they are the parameters required by the method. You can pass this by yourself.

2. Arrow function pointing: The arrow function does not have a name, so it cannot be called by name, so this always points to Window.

3. Variable pointing: I think this is involved Problem with memory pointers. But it is easy to understand, that is, constants occupy memory, and variables are added up. It's like 2+ children playing a game. As long as you don't change places or play with other people, what's yours is mine, and what's mine is yours. This memory is like a children's play field, and the toys owned by the children are their attributes (these children are relatively generous).
Let’s give three examples:

var xArr = [];
var xJson = {};
(()=>{
      let yArr = xArr,
         yJson = xJson;
       yArr.push(1);
       yJson.age = 1;
      })();//这里说明即便是块级变量也是可以一起参与玩耍的,屋里玩耍的孩子玩具一样可以被其他小孩在屋外展示。
console.log(xArr);//[1]; 
console.log(xJson);//{age: 1}
Copy after login
Copy after login

Because Y has never found anyone else to play with (see example 3 for how to find others to play with), so y’s toys are x’s toys.

var x = 0,
    a = 2.
    b = 3,
    y = x;
    console.log(y);//0
    y = a+b;
    console.log(x);//0
    console.log(y);//5
Copy after login
Copy after login

Because Y changed the place to play (opened a memory to point to), so x cannot get Y's toys.

var x = {},
    a = {},
    y = x,
    z = y,
    y = a;
    y.age = 1;
    console.log(x);//{}
    console.log(y);//{age:1}
    console.log(z);//{}
    console.log(a);//{age:1}
    z.age = 2;
    console.log(x);//{age:2}
Copy after login
Copy after login

Related recommendations:

Comprehensive analysis of this in JavaScript

How to use this in js

A brief introduction to this rule in JavaScript

The above is the detailed content of js pointing problem. For more information, please follow other related articles on the PHP Chinese website!

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

What are the questions in the Rulong 8 Wine Master exam? What are the questions in the Rulong 8 Wine Master exam? Feb 02, 2024 am 10:18 AM

What are the questions involved in the Yulong 8 Wine Master exam? What is the corresponding answer? How to pass the exam quickly? There are many questions that need to be answered in the Master of Wine Examination activities, and we can refer to the answers to solve them. These questions all involve knowledge of wine. If you need a reference, let’s take a look at the detailed analysis of the answers to the Yakuza 8 Wine Master exam questions! Detailed explanation of answers to questions in the Rulong 8 Wine Master exam 1. Questions about "wine". This is a distilled liquor produced by a distillery established by the royal family. It is brewed from the sugar of sugarcane grown in large quantities in Hawaii. What is the name of this wine? Answer: Rum 2. Question about "wine". The picture shows a drink made from dry ginseng and dry vermouth. It is characterized by the addition of olives and is known as "cockney"

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

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

See all articles