Person.prototype.sayHello = function() { console.log(`Hello, my name is ${this.name}`); };
const person1 = newPerson("Alice"); person1.sayHello(); // 输出: Hello, my name is Alice
上面的例子创建了一个名为Person的构造函数,并将prototype上的sayHello设置为一个打招呼的函数。prototype是Person的一个属性,所有用类Person进行实例化的对象,都会拥有prototype的全部内容。所以当创建一个名为person1的实例时,它会继承Person.prototype对象上的sayHello方法。因此,当调用person1.sayHello()时,会输出“Hello, my name is Alice”。
// GET /api/store - Retrieve the current KV store app.get("/api/store", (req, res) => { res.json(store); });
// POST /set - Set a key-value pair in the store app.post("/set", (req, res) => { const { key, value } = req.body;
const keys = key.split("."); let current = store;
for (let i = 0; i < keys.length - 1; i++) { const key = keys[i]; if (!current[key]) { current[key] = {}; } current = current[key]; }
// Set the value at the last key current[keys[keys.length - 1]] = value;
res.json({ message: "OK" }); });
// GET /get - Get a key-value pair in the store app.get("/get", (req, res) => { const key = req.query.key; const keys = key.split(".");
let current = store; for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (current[key] === undefined) { res.json({ message: "Not exists." }); return; } current = current[key]; }
res.json({ message: current }); });
// GET /execute - Run commands which are constant and obviously safe. app.get("/execute", (req, res) => { const key = req.query.cmd; const cmd = cmds[key]; res.setHeader("content-type", "text/plain"); res.send(execSync(cmd).toString()); });