In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Javascript's caller,callee,call,apply". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Javascript's caller,callee,call,apply.
1. CallerJScript reference: returns a reference to a function that calls the current function. How to understand this sentence, let's first give a simple example: / / caller demo {function callerDemo () {if (callerDemo.caller) {var a = callerDemo.caller.toString (); alert (a);} else {alert ("this is a top function");}} function handleCaller () {callerDemo () } in the above example, you can see that it is a reference that returns the call data. (pointing to the function to be called) you can also see that when this is the case, the description in the calleeJScript reference is: return the Function object being executed, that is, the body of the specified Function object.
It is important to note that callee has the length attribute, which is sometimes good for validation.
Function calleeDemo () {alert (arguments.callee);} function calleeLengthDemo (arg1, arg2) {if (arguments.length==arguments.callee.length) {window.alert ("verify that the parameters and arguments are correct in length!") ; return;} else {alert ("argument length:" + arguments.length); alert ("parameter length:" + arguments.callee.length);}} you can see from the above example that callee can be used to execute a function, that is, to point to the called function. The above example shows that calee can print itself, and of course there are other uses. In the length attribute, arguments.length is the argument length, and arguments.callee.length is the parameter length, so you can determine whether the parameter length is consistent with the argument length when calling. 3. Instructions in the JScript reference for call and applycall methods: call a method of one object and replace the current object with another. Call ([thisObj [, arg1 [, arg2 [, [, .argN]), but without the instructions in the example apply method JScript reference: apply one method of one object and replace the current object with another. Apply ([thisObj [, argArray]]) actually has almost the same effect. It is important to note that the arg parameter in call (thisObj [, arg1 [, arg2 [,) can be a variable, while the parameter in apply ([thisObj [, argArray]]) is an array collection. Let's take a look at call, the specific application of apply / / simple call demofunction simpleCallDemo (arg) {window.alert (arg);} function handleSPC (arg) {simpleCallDemo.call (this, arg);} / / simple apply demofunction simpleApplyDemo (arg) {window.alert (arg);} function handleSPA (arg) {simpleApplyDemo.apply (this, arguments) } as you can see from the simple example above, call and apply can call the application of another function by passing the current parameter to the argument of another function. Sometimes this is a very practical method, of course, using call or apply (parameters or arrays) depends on the actual situation. Let's take a look at another skill in the application of call and apply, that is, after applying another function (class) with call and apply, the current function (class) has the methods or properties of another function (class), which can also be called "inheritance". Look at the example below. / / inheritfunction base () {this.member = "never-online"; this.method = function () {window.alert (this.member);}} function extend () {base.call (this); window.alert (member); window.alert (this.method);} the above example shows that extend can inherit the methods and properties of base through call. Look at an apply app / / advanced apply demofunction adApplyDemo (x) {return ("this is never-online, BlueDestiny'" + x + "'demo");} function handleAdApplyDemo (obj, fname, before) {var oldFunc = obj [fname]; obj [fname] = function () {return oldFunc.apply (this, before (arguments);};} function hellowordFunc (args) {args [0] = "hello" + args [0]; return args } function applyBefore () {alert (adApplyDemo ("world"));} function applyAfter () {handleAdApplyDemo (this, "adApplyDemo", hellowordFunc); alert (adApplyDemo ("world")); / / Hello world!} it is important to note that if you click the "original adApplyDemo ('world')" button first, if you click the "applied adApplyDemo (' world')" button, the apply method will be applied first, so the original value will be changed. Perhaps some friends do not find anything special. I would like to point out here that when you click the button on the left, there is only "this is never-online, BlueDestiny 'world' demo". When you click the button on the right, the result will be "this is never-online, BlueDestiny' hello world' demo". Click the button on the left to see what the result will be. Try it yourself: d, the function adApplyDemo has been rewritten. This example illustrates the "real" role of call and apply. At this point, I believe you have a deeper understanding of "how to use Javascript's caller,callee,call,apply". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.