Announcement

Collapse
No announcement yet.

Pass By Reference, Value, Declaration:

Collapse
X
Collapse
 

  • JavaScript
    started a blog post Pass By Reference, Value, Declaration:

    Pass By Reference, Value, Declaration:

    Pass By Reference, Value, Declaration:

    Function Context:
    • A function's context is comprised of four kinds of scope:
    • * Global Scope — all functions can access the global scope
    • * Local Scope — all variables declared with var within the function or const/let declared variables not declared within a lexical child statement block within the function
    • * Block Scopes — all variables declared with const/let within a statement block within the function
    • * Closure Scopes — all variables defined within the local scope of the enclosing function
    • Passing Data to Functions:

    Data can be passed to function using three methods:
    • Pass By Reference
    • Pass By Value
    • Pass By Declaration

    Pass By Reference:
    • When a variable containing a reference to an object is passed as an argument to a function invocation, a pass by reference operation occurs
    • While the passing of the object reference itself is a pass by value (the value of the object reference), the practical usage of the parameter results in a pass by reference
    • When the object's property is referenced on the parameter, the passed object referenced is used to reference the object on the heap
    • Any changes to this object will impact all variables in the application which reference the object
    • Pass By Value:
    • When a variable containing a primitive value is passed as an argument to a function invocation, a pass by value operation occurs
    • The value of the variable is copied to the named parameter of function
    • The named parameter value can be changed, but the change does not impact the original variable's value passed in as an argument to the function call
    • Pass by value is the simplest method to pass data into a function
    • Pass By Declaration:
    • Occurs when a variable is referenced in a function from the outer lexical scope (closure scopes & outer block scopes)
    • When the function is invoked, to access the variable, the scope needs to be loaded first, then the value can be retrieved or modified
    • For closure scopes and outer block scopes, this can be an expensive operation, as the scope of the variable must be reloaded before the variable itself can be accessed
    • Also, keeping the context of closure and outer block scopes consumes additional memory
    Click image for larger version

Name:	Screenshot 2021-09-10 at 3.22.40 PM.png
Views:	42
Size:	350.2 KB
ID:	605

    • Arun
      #1
      Arun commented
      Editing a comment
      Thanks, I got the idea, can you add some examples ?
    You must be logged in to post a comment.

Related Topics

Collapse

Working...
X