Home > Web Front-end > JS Tutorial > body text

This in Js points to the problem of apply().call(),bind()

一个新手
Release: 2017-10-23 10:02:37
Original
1453 people have browsed it

Reason for note recording: The popular trend of JavaScript has been unstoppable. The derived AngularJs, Node.js, and BootStrmp have less and less advantages in back-end development for small and medium-sized enterprises. IT is not a position that can rely entirely on experience. IT changes life. As a .Net programmer for three years, I now deeply feel that being conservative is actually fear, avoiding the progress of the times. Life requires passion and motivation!

Text:

1. What do apply().call() and bind() do?

These three are all changed to what this points to in the method

2. What is this?

This refers to the execution object of the method you are currently calling, such as the setTimeout method The output of this


##

1 setTimeout(function(){
2                 console.log(this)
3            },1000)
Copy after login

is:


Window {external: Object, chrome: Object, document: document, Alax: Object, a2: Object…}
Copy after login

Because setTimeout is completely called when Window.setTimeout it belongs to the Window object a method.

Define another object


var Alax={
         name:'Alax',
         age:27,
         fuc:function(){            
         return this.age;
         }
        }
Copy after login

It stands to reason that using console.log(Alax.fun) should output 27 but the actual situation is undefined, why? Woolen cloth? Because when called outside the Alax object, the actual object is Window, and Window does not contain the age attribute, which means that the this pointer has changed and needs to be respecified. Remember to wait until the concept is finished to compare what you think

3. The difference between call, apple and bind

1. Parameter comparison

The parameter format of call is (thisvalue,arg1)

apply’s It is (thisvalue,[arg1,ar2...])

The parameter format of bind is (thisvalue,arg1,ar2...)

Thisvalue refers to the this pointer to be passed, The following parameters are optional and correspond to the parameters of the method one-to-one. If they are passed in, they can be obtained using arguments

2. Return result comparison

Call and apple refer to passing all parameters to arguments , and directly return the call result

Bind refers to returning an incoming value and returning a new one after the method runs

The function needs to add () call

Summary: The above calling method can be


console.log((Alax.fuc).call(Alax))
console.log((Alax.fuc).apply(Alax))
console.log((Alax.fuc).bind(Alax)())
Copy after login

Attach parameter call


Alax={
         name:'Alax',
         age:27,
         fuc:function(num){               
         this.age+=num               
         return this.age;
         }
    }
 console.log((Alax.fuc).call(Alax,1))
 console.log((Alax.fuc).apply(Alax,[1]))
 console.log((Alax.fuc).bind(Alax)(1))
Copy after login

The above is the detailed content of This in Js points to the problem of apply().call(),bind(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!