着信者及び発信者
2022-02-19 22:41:13
前回の記事で 引数 と、その属性の呼び出し側と、似たような外観の双子の兄弟呼び出し側である
まずはcalleeから、コードのスニペットで見てみましょう。
var a=function(){
console.log(arguments.callee);
}
var b=function(){
a()
}
b();
このコードをクロームで表示すると、次のように出力されます。
ƒ (){
console.log(arguments.callee);
}
この関数はa関数と全く同じですか?
callee returns a reference to the function itself. callee is an attribute of arguments, which is a pointer to the function that has the arguments object (arguments is the calling function, so the callee is the calling function)
Let's start with an example of solving with a callee, the factorial calculation.
function factorial(num){
if(num<=1){
}else{
return num*factorial(num-1);
}
}
The code above is using the function name. The above method will enhance coupling, to reduce coupling we will use callee, and the recursive call can be done regardless of the name of the referenced function. Look at the callee.
function factorial(num){ if(num<=1){ return 1; }else{ return num*arguments.callee(num-1); } }
Similar to him we have a caller, which is normalized in es5 with another function object property caller; this property holds the function reference of the current function called, and returns null if the current function is called in the global scope
function outer(){ inner(); } function inner(){ 1. alert(arguments.callee.caller); 2. alert(inner.caller) // these two lines above are the same 1 is more loosely coupled than 2 3. alert(arguments.caller);//undefined // 3. This line of code will report an error in strict mode, but in non-strict mode it is undefined } outer(); // It says that the argumnets.callee property is defined to distinguish between qrguments.caller and caller // You can't assign a value to the caller property in strict mode, otherwise you get an error // var a=function(){ // alert(a.caller); // }// Define a function. Inside output a.caller // var b=function(){ // a(); // }// Define a function to call that a function. // b(); // output b function. In the example above, it is the b called that knows the answer based on the properties of the caller. // var a=function(){ // alert(a.caller); // }// Define a function. Inside output a.caller // var b=function(){ // a(); // }// Define a function to call that a function. // a(); // null (a is called in any function, that is, the top-level function, the output is null), the above example one, a function is called in b, so it is not the top-level does not regret null, return the current call is b function, and this example is called in the global natural is null One last example to help provide insight into the top level: if there is a function in a, too
の呼び出し元となります。var c=function(){ alert(c.caller); } var a=function(){ c() alert(a.caller); } var b=function(){ a(); } a(); The reason this example's alert comes out of the a function in c is because it's called in a. The alert in a comes out null, after all you are calling the a function globally. the b function is equivalent to not using it. Well, if you don't get it with all these examples, maybe I'm just a bad writer.
function outer(){ inner(); } function inner(){ 1. alert(arguments.callee.caller); 2. alert(inner.caller) // these two lines above are the same 1 is more loosely coupled than 2 3. alert(arguments.caller);//undefined // 3. This line of code will report an error in strict mode, but in non-strict mode it is undefined } outer(); // It says that the argumnets.callee property is defined to distinguish between qrguments.caller and caller // You can't assign a value to the caller property in strict mode, otherwise you get an error
// var a=function(){ // alert(a.caller); // }// Define a function. Inside output a.caller // var b=function(){ // a(); // }// Define a function to call that a function. // b(); // output b function.
In the example above, it is the b called that knows the answer based on the properties of the caller.
// var a=function(){ // alert(a.caller); // }// Define a function. Inside output a.caller // var b=function(){ // a(); // }// Define a function to call that a function. // a(); // null (a is called in any function, that is, the top-level function, the output is null), the above example one, a function is called in b, so it is not the top-level does not regret null, return the current call is b function, and this example is called in the global natural is null
One last example to help provide insight into the top level: if there is a function in a, too
var c=function(){ alert(c.caller); } var a=function(){ c() alert(a.caller); } var b=function(){ a(); } a();
The reason this example's alert comes out of the a function in c is because it's called in a.
The alert in a comes out null, after all you are calling the a function globally. the b function is equivalent to not using it.
Well, if you don't get it with all these examples, maybe I'm just a bad writer.
関連
-
[解決済み】Javascriptのswap配列要素
-
[解決済み] Webアプリでfacebook共有を実装する際にエラーが発生する パラメータ app_id is required
-
[解決済み] sublimeでのreactコードのシンタックスハイライト
-
[解決済み] AddclassはjQueryの関数ではありません【終了しました
-
[解決済み] DynamoDBで予約キーワードをFilterExpressionとしたスキャン関数 NodeJS
-
[解決済み] 迷走する開始タグスクリプト
-
[解決済み] ボタンではなく、リンクを使ってモーダルポップアップを開く方法
-
[解決済み] Number.EPSILONの利用シーンはどのようなものがありますか?
-
JSLint を使用して Javascript をチェックすると、parseInt が Warning を報告します。基数パラメータがありません
-
非捕捉(プロミス内)DOMExceptionエラー
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
[解決済み】「次へ」ボタンが押されると、html5ビデオを自動的に一時停止する(javascript/jquery)。
-
[解決済み] 周期的なオブジェクト値を含むオブジェクトのシリアライズ
-
[解決済み] three.jsで背景を透明にする
-
[解決済み] LocalStorageにboolean値を設定できない?
-
[解決済み] electron 5.0.0 "Uncaught ReferenceError: require is not defined".
-
[解決済み] 永遠にループし、デルタタイムを提供する
-
[解決済み] JavaScriptで要素に複数の属性を一度に設定する
-
[解決済み] HTML5 Canvas の z-index を設定する
-
[解決済み] JavascriptのappendChild()が動作しない。
-
[解決済み] jQuery scrollTop()メソッドが動作しない