https://www.quora.com/What-is-the-meaning-of-context-in-programming

在编程中,”上下文“一词语指的是执行特定代码或函数时所处的情况或环境。上下文提供了影响代码行为的重要信息和条件。它可以包括各种因素、变量值、程序执行中的位置以及当时接收到的外部输入。

以下是编程中常见的几种上下文:

  1. 执行上下文:该上下文指的是执行特定代码的环境。它包括有关变量、函数及其存在范围的信息。执行上下文有助于编程语言在程序执行过程中跟踪变量值和函数调用
  2. 函数中上下文:在函数中,上下文通常指 this 关键字的值。this 关键字指的是函数作为属性的对象。它的值由函数的调用方式决定。在面向对象编程中,理解 this 关键字的上下文至关重要,它极大地影响函数的行为。
  3. 网络开发中的上下文: 在网络开发中,上下文通常是指 JavaScript 代码在网络浏览器中执行的上下文。例如,代码可以在全局上下文(整个文档)、函数或特定 DOM 元素的上下文中执行。
  4. 多线程中的上下文:在多线程环境中,上下文通常指线程的状态,包括其寄存器、程序计数器和堆栈指针。切换线程的上下文可以让多任务操作系统高效地管理多个线程。

了解上下文对于编写高效、无错误的代码至关重要。它可以帮助程序员根据代码的运行环境来预测代码的行为。不同的编程语言和环境都有关于上下文的特定规则和行为,因此开发人员在编写代码时必须了解这些细微差别。

原文:

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.

什么是上下文

Java中的上下文是什么?