java-script:function-context
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java-script:function-context [2023/08/09 14:46] – odefta | java-script:function-context [2023/08/09 14:52] (current) – odefta | ||
---|---|---|---|
Line 218: | Line 218: | ||
</ | </ | ||
- | ==== How to access the context of a parent | + | ==== How to access the context of a parent |
- | Here we'll need to assign the context of the parent | + | Here we'll need to assign the context of the parent |
Example: | Example: | ||
<code javascript> | <code javascript> | ||
- | const parentFunction | + | const parentObject |
parentValue: | parentValue: | ||
childFunction: | childFunction: | ||
Line 238: | Line 238: | ||
}; | }; | ||
- | parentFunction.childFunction(); | + | parentObject.childFunction(); |
</ | </ | ||
- | ==== How to access the context of a parent | + | ==== How to access the context of a parent |
Using an **arrow function** can simplify the code because arrow functions **don' | Using an **arrow function** can simplify the code because arrow functions **don' | ||
Line 247: | Line 247: | ||
<code javascript> | <code javascript> | ||
- | const parentFunction | + | const parentObject |
parentValue: | parentValue: | ||
childFunction: | childFunction: | ||
Line 259: | Line 259: | ||
}; | }; | ||
- | parentFunction.childFunction(); | + | parentObject.childFunction(); |
</ | </ | ||
- | By using an arrow function for the inner function, we automatically capture the value of this from the enclosing childFunction method. This allows the inner function to access the properties and methods of the parentFunction object | + | By using an arrow function for the inner function, we automatically capture the value of this from the enclosing childFunction method. This allows the inner function to access the properties and methods of the parentObject |
\\ It's a cleaner and more concise way to write code that depends on maintaining the context of this across different levels of nested functions. | \\ It's a cleaner and more concise way to write code that depends on maintaining the context of this across different levels of nested functions. | ||
java-script/function-context.1691581619.txt.gz · Last modified: 2023/08/09 14:46 by odefta