API
一句话解释
API (Application Programming 接口) 是设置的 规则那个定义如何不同 software 组件 communicate 使用每个 other.
用大白话说
API 是一种contract between two pieces of software。 It defines what functions you can call, what arguments they take, and what they return — without revealing how they work internally. You use APIs every day without thinking about it.
例如, the JavaScript console.log() is an API: you call it with a string, it prints to the console. You do not know (or need to know) how it actually writes to the terminal. Web APIs like 获取() let you make HTTP requests without understanding TCP/IP.
APIs come in many forms: language standard libraries (built-in functions), web APIs (REST endpoints you call over HTTP), library APIs (functions from npm/pip packages), and operating system APIs. The key idea is always the same: a defined interface that hides implementation details.
示例
// Using a web API (fetch)
async function getUser(id) {
const response = await fetch(`/api/users/${id}`);
const user = await response.json();
return user;
}
// The fetch API hides all the HTTP/TCP complexity
// You just call it with a URL and get data back获取() 是 API: 你通过 URL, 它返回响应. 所有 networking 复杂度是 隐藏后面这个简单接口.
相关术语
相关资源
相关课程
- 模块 and 导入
学习关于模块 and APIs.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。