https://www.quora.com/What-is-the-meaning-of-context-in-programming
在编程中,”上下文“一词语指的是执行特定代码或函数时所处的情况或环境。上下文提供了影响代码行为的重要信息和条件。它可以包括各种因素、变量值、程序执行中的位置以及当时接收到的外部输入。
以下是编程中常见的几种上下文:
了解上下文对于编写高效、无错误的代码至关重要。它可以帮助程序员根据代码的运行环境来预测代码的行为。不同的编程语言和环境都有关于上下文的特定规则和行为,因此开发人员在编写代码时必须了解这些细微差别。
原文:
In programming, the term "context" refers to the circumstances or environment in which a particular piece of code or a function is executed. The context provides important information and conditions that affect the behavior of the code. It can include various factors such as the state of the system, the values of variables, the location within the program's execution, and the external inputs received at that moment.
Here are a few common contexts in programming:
1. Execution Context: This context refers to the environment in which a particular section of code is executed. It includes information about variables, functions, and the scope in which they exist. The execution context helps the programming language keep track of variable values and function calls during program execution.
2. Context in Functions: In functions, context often refers to the value of the this keyword. The this keyword refers to the object that a function is a property of. Its value is determined by how a function is called. Understanding the context of this is crucial in object-oriented programming and can greatly affect the behavior of functions.
3. Context in Web Development: In web development, context often refers to the context in which JavaScript code is executed within a web browser. For example, code can be executed in the global context (the entire document), within a function, or in the context of a specific DOM element.
4. Context in Multithreading: In multithreading environments, context often refers to the state of a thread, including its register values, program counter, and stack pointer. Switching the context of threads allows a multitasking operating system to manage multiple threads efficiently.
Understanding the context is essential for writing effective and bug-free code. It helps programmers anticipate the behavior of their code based on the circumstances in which it operates. Different programming languages and environments have specific rules and behaviors regarding context, so it's crucial for developers to be aware of these nuances when writing code.