[Effective JavaScript note] 29th: Avoid using non-standard stack check properties

Source: Internet
Author: User
Tags stack trace

Many JS environments provide the ability to check the call stack. The call stack refers to the active function chain that is currently executing. In some old hosting environments, each arguments object contains two additional attributes: Arguments.callee and Arguments.caller. The former points to the function that is called using the arguments object. The latter points to a function that invokes the function called by the arguments object. Many environments support Arguments.callee, but it does not have much use in addition to allowing anonymous functions to recursively refer to themselves. (High 3 thinks that using Arguments.callee can relieve the coupling between the code and the function name in the body, and it does not seem completely useless)

Icon

Here is a simple diagram that makes it easy to understand the callee,caller of arguments and the caller of functions

var factorial= (function (n) {    return (n<=1) 1: (N*arguments.callee (N-1));})

But this is also particularly useful, you can use the function name to refer to the function itself

function factorial (n) {    return (n<=1) 1: (N*factorial (N-1));}

The Arguments.caller property is more powerful. It points to a function that invokes a function using the arguments object. For security reasons, most environments have removed this feature, so it is necessary to detect it when using it. Many JS environments also provide a similar function object property-non-standard but universally adaptable caller properties. It points to the nearest caller of the function.

function Revealcaller () {    return revealcaller.caller;} function Start () {    return Revealcaller ();} Start () ===start;//true;

You can use this property to get a data structure that provides a snapshot of the current call stack. Build a stack trace:

function Getcallstack () {    var stack=[];    for (Var f=getcallstack.caller;f;f=f.caller) {        stack.push (f);    }    return stack;}

Using the example

Function F1 () {    return getcallstack ();} function F2 () {    return F1 ();} var trace=f2 ();//[f1 (), F2 ()]

Vulnerability, when a function appears more than once in the call stack, the stack-check logic will fall into the loop.

function f (n) {    return n===0?getcallstack (): F (n-1);} var trace=f (1);//

What's the problem? Since function f recursively calls itself, its caller property is automatically updated, referring back to function f. Therefore, the function Getcallstack will fall into the loop of finding the function f indefinitely. Even though we tried to detect the loop, there was no information about which function called it before the function f called itself. Because the information for the other call stacks has been lost.
These stack check properties are non-standard and are restricted in portability or applicability. In ES5 's strict-mode functions, they are forbidden to use. Attempting to get the caller or callee property of a strict function or arguments object will be an error.

function f () {  ' use strict ';  return F.caller;} f ();//uncaught TypeError: ' Caller ' and ' arguments ' are restricted function properties and cannot is accessed in this Conte Xt. (...)

The best strategy is to completely avoid stack checking. If the reason for checking the stack is purely for testing, then the more reliable way is to use the interactive debugger.

Tips
    • Avoid non-standard arguments.caller and Arguments.callee attributes that do not have good portability

    • Avoid using nonstandard function object Caller property, because it is unreliable in containing all stack information

Appendix: This does not have the appendix, water one article, because this section really has nothing to write.

[Effective JavaScript note] 29th: Avoid using non-standard stack check properties

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.