Home  >  Article  >  Web Front-end  >  This in Js points to the problem of apply().call(),bind()

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

一个新手
一个新手Original
2017-10-23 10:02:371333browse

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)

is:


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

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

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

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

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!

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