


How to change the value pointed to by this in a function in JavaScript?
fun.apply(context,[argsArray])
Call fun immediately, and at the same time point the original this of the fun function to the new context object passed in, implementing the same method in different reused on the object.
context: the object passed in, replacing the original this of the fun function;
argsArray: an array or array-like object, in which the array parameters will be expanded and passed to fun as separate actual parameters For functions, you need to pay attention to the order of parameters.
fun.call(context,[arg1],[arg2],[…])
Same as apply, except that the parameter list is different. The parameters of call need to be separated. An incoming. If you don't know the number of parameters, use apply.
Use:
Math.max() //Only receive individual parameters, you can use the max method on the array through the following method:
Math.max.apply(null, array); //Will expand the array array parameters into separate parameters and then pass them in
Array.prototype.push.apply(arr1,arr2); //Split one array and push it into another array; no need to apply Then the subsequent array parameters will be pushed in as an element.
Array.prototype.slice.call(arguments); //Use the slice method on the class element group object
##
function isArray(obj){ return Object.prototype.toString.call(obj) === '[object Array]' ; } //验证是否是数组
fun.bind (context,[arg1],[arg2],[…])
Make the context executed by the fun method never change. arg1: Argument list to be passed to the new function Returns a function for subsequent calls. Its function body is the same as the original function fun, but the this of the new function points to the newly passed in context object. . The new function will have the initial parameters arg1/arg2... specified by the bind method. The actual parameters when subsequently calling the new function will be arranged behind the existing parameters.//原来的函数有4个参数 var displayArgs = function (val1, val2, val3, val4) { console.log(val1 + " " + val2 + " " + val3 + " " + val4); } var emptyObject = {}; // 生成新函数时bind方法指定了2个参数,则新函数会带着这个两个实参 var displayArgs2 = displayArgs.bind(emptyObject, 12, "a"); // 调用时传入另2个参数,要在bind方法传入的2个实参后面 displayArgs2("b", "c"); // Output: 12 a b c
var obj = { arg1 : 1, attach: function(){ //var self = this; 普通传入this 的方法 $('xxx').on('click',function (event) { console.log(this.arg1);//若不绑定this,回调函数中的this常指目标元素 }.bind(this)); //使用bind方法绑定this } }
var _Slice = Array.prototype.slice; var slice = Function.prototype.call.bind(_Slice); slice(…);
if (!Function.prototype.bind) { Function.prototype.bind = function(context) { var self = this, // 调用bind方法的目标函数 args = arguments; return function() { self.apply(context, Array.prototype.slice.call(args, 1));//参数个数不确定时用apply } } }
this
#This object is bound based on the execution environment of the function when the function is running: in the global function, this is equal to window, and when the function is treated as When a method of an object is called, this is equal to that object. Judgment method: this has nothing to do with where it is defined. When the function is running, if there is a . operator, this refers to the object before .; if not, this refers to the window. If the new keyword is called, it refers to a new object. When there is apply/call/bind, it refers to the first parameter./*例1*/ function foo() { console.log( this.a ); } var obj2 = { a: 42, foo: foo }; var obj1 = { a: 2, obj2: obj2 }; obj1.obj2.foo(); // 42;当foo函数被调用时,其本身是归obj2所拥有 /*例2*/ function foo() { console.log( this.a ); } var obj = { a: 2, foo: foo }; var bar = obj.foo; // bar引用foo函数本身 var a = "global"; // 全局对象的属性 bar(); // "global" ;
The above is the detailed content of How to change the value pointed to by this in a function in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

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



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.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

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

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

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 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

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

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
