In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Javascript implementation of call,bind,apply code how to write the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Javascript code to achieve call,bind,apply how to write the article will have a harvest, let's take a look.
1. Check whether the current call is a function
two。 If no this is currently passed in to point to, the value is assigned to window
3. Point fn to the currently called function
4. Get the parameters passed in
5. Pass parameters into fn for calling
6. Delete the fn on the object
7. Return the result
/ / implementation of ordinary call function hello () {console.log ('hello I am' + this.name);}; let person = {name:'krys'}; var name = 'liang';// only the variable of var belongs to window hello (); / /' hello I am liang' hello.call (person); / / 'hello I am krys' hello.call () / / 'hello I am liang' let person2 = {name:'lwl'} Function.prototype.mycall = function (context) {/ / when no parameter is passed, the default is window if (typeof this! = = "function") {throw new TypeError (' Error');} context = context | | window; context.fn = this;//fn is the above hello method const args = [. Arguments] .slice (1) / / the first parameter is not const result = context.fn (.. ARGs); / / pass the remaining parameters to hello delete context.fn; return result;} hello.mycall (person2); function getParams () {console.log ('I am', this.name,' gets some parameters',.. parameters);} let person3 = {name:'hhh'} GetParams.apply (person3, ['hello','world']) Function.prototype.myApply = function (context) {if (typeof this! = = "function") {throw new TypeError ()} context = context | | window; context.fn = this; let result If (arguments [1]) {/ / if there is a parameter array console.log (arguments [1]) result = context.fn (. Arguments [1]);} else {result = context.fn ();} delete context.fn; return result } getParams.myApply ({name:'llll'}, ['jjj','kkkk','llll']); function getParams () {console.log (' I am', this.name,' gets some parameters',.. parameters);} let person3 = {name:'hhh'}; let person4 = {name:'tttt'} GetParams.bind (person3,'hello','world') getParams.bind (person4,'hello','world') ('jjj','kkk'); Function.prototype.myBind = function (context) {if (typeof this! = = "function") {throw new TypeError ()} context = context | | window; const _ that = this; const args = [. Arguments] .slice (1) Return function F () {if (this instanceof F) {return new _ that (... args,...arguments); / / the arguments here is the above jjj kkk} return _ that.apply (context,args.concat (.. steps)). / / the arguments here is the jjj kkk}} getParams.myBind ({name:'llll'}, 'jjj','kkkk','llll') (' hhhhllll') above; this is the end of the article on "how to write the code for Javascript to implement call,bind,apply". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to write the code for Javascript to achieve call,bind,apply". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.