Where will bind,call,apply be used in real projects?

often see articles that introduce call or apply, and then use the following code as an example.

function identify() {
  console.log("Hello,I"m " + this.name);
}
let me = {
  name: "Kyle"
};
let you = {
  name: "Reader"
};
identify.call(me); // Hello,I"m Kyle
identify.call(you); // Hello,I"m Reader

what I don"t understand is that in actual development, wouldn"t it be better to pass the indentify (obj) method in the form of parameters,

// 
function identify(obj) {
  console.log("Hello,I"m " + obj.name);
}
let me = {
  name: "Kyle"
};
let you = {
  name: "Reader"
};
identify(me); // Hello,I"m Kyle
identify(you); // Hello,I"m Reader

so where will bind,call,apply be used in actual projects?

Mar.24,2021

I also agree that the second method is better. In fact, I think that JavaScript does not give this a formal parameter status is a design error , other functional / object-oriented mixed languages are explicitly given (so that they are ridiculed by Python, new languages dare not use the keyword this . If), TypeScript refers to this outside class , it is also required to be explicitly declared as the first formal parameter. The compiler knows that this is a hidden parameter, and so do programmers, but everyone makes a fortune in silence, which is the worst.

< H1 > bind / call / apply for what < / H1 >

bind can be used as a partial function application, try to continue to fill in the parameters, these parameters will be "remembered" for later use.

in the modern context, the only function of call / apply is to extract this , which is originally a hidden parameter, into a formal parameter, allowing programmers to specify explicitly. The example given above is Math.max.apply (null, [1,2,3,4]) . The recommended approach should be Math.max (. [1,2,3,4]) , which is clear and slightly improved in modern browsers. Unless someone else's code is quoted, with all due respect, voluntarily messes with call / apply is civilized language behavior .

< H1 > encapsulated for others to use / code written by others < / H1 >

personal observation, the current general trend is not to do civilized language behavior, not to mess around this outside class , so can expect less and less call / apply in the code written by others. Koa 1 was sprayed with this context, and koa 2 was immediately changed to an explicit parameter. The tool library of lodash is all passed parameters in the second way, and if you want to chain call, you need to wrap it manually.

vue A lot of this , in fact, a small group of people have a problem with it. Considering that generally speaking, the functions of components are not universal, and there is no need for call / apply , I accept it. But vuex's mutations is likely to be universal, so it is also the second way to pass parameters, otherwise it is really a super civilized language . If you write your own code and need someone else to provide this , it is recommended to reflect on it.

< H1 > unbound < / H1 >

the above answer mentions "anti-Coriarization implementation" (in fact, I think debound), is more of a compromise on issues left over from history . Designed as a general-purpose function, it should not be tied to a particular class, but this has already happened, and the ES standard cannot be greatly changed. Compatible dog-feeding languages like Python have completely eliminated the need for "anti-Coritization" behavior, all directly passing parameters in the second way. It's not because of this , there's no need to ask for trouble.

< H1 > Summary < / H1 >
  • call / apply is the solution to problems left over by history
  • bind is more widely used, but partial functions are quite easy to use
  • .
  • in order to use those old libraries, it is still necessary to learn call / apply
  • .
  • Don't involve call / apply in your own code.

personally feel that they seldom use it when writing their own code, but


is mainly related to this about identify written by you , but if it is a third-party plug-in written by someone else


can only say that this example is only for a clearer understanding of call , apply .
in fact, call and apply are more often used to do anti-Corialization implementations (a previously written article on Corification and de-Corialization ).

  A simple js plug-in  

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3d3e5-2c3a7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3d3e5-2c3a7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?