Consolidated guidance for completing Week 1 Git & JavaScript lab activities. 汇总第1周 Git 与 JavaScript 实验活动的操作要点与技巧。
Use this note as a quick reference before or during the lab. Each section highlights the commands, reasoning, and collaboration habits expected in tutorials. 本笔记可在实验前或实验中快速参考,涵盖命令、思路与协作习惯,帮助你在课堂上高效完成任务。
git add filename暂存更改:git add filenamegit commit -m "message"提交并写明含义:git commit -m "message"git push推送至远端:git pushgit push -u origin main for the first push so future git push works alone.上游跟踪:首次推送使用 git push -u origin main,之后直接 git push 即可。git status before and after committing.状态检查:提交前后都运行 git status。Simulate a teammate updating the repository to practise pulling safely. 模拟队友更新仓库,练习安全地拉取最新代码。
git pull回到本地仓库执行 git pullWeek 1 labs emphasise modelling state with plain objects. Keep the following patterns handy. 第1周实验强调使用普通对象描述状态,下面的模式可以随手参考。
// Object literal
const profile = {
name: 'John Doe',
age: 25,
createdAt: 1234567890
};
Remember: dot syntax for reading/updating, and spread syntax for cloning. 牢记:用点语法读取/更新,用展开语法克隆对象。
// Current Unix timestamp (seconds)
const now = Math.floor(Date.now() / 1000);
// Human readable helper
function formatTimestamp(ts) {
return new Date(ts * 1000).toISOString();
}
Mnemonic: “JavaScript gives milliseconds, divide by 1000 for seconds.” 记忆:“JS 给的是毫秒,除以 1000 变成秒”。
updatedAt).遵循驼峰命名:首词小写,其余首字母大写(如 updatedAt)。in operator to confirm property existence: 'updatedAt' in profile.使用 in 操作符确认属性存在:'updatedAt' in profile。age.保持数字字段为数字,避免用模板字符串生成如 age 这类数值。Builds a profile object with name, age, and created timestamp. 创建带姓名、年龄与创建时间的档案对象。
function profileCreate(nameFirst, nameLast, birthYear) {
const currentYear = new Date().getFullYear();
const timestamp = Math.floor(Date.now() / 1000);
return {
name: `${nameFirst} ${nameLast}`,
age: currentYear - birthYear,
createdAt: timestamp
};
}
Returns a positive, negative, or zero value when comparing ages. 比较两人年龄,返回正数/负数/零。
function profileCompareAge(profile1, profile2) {
return profile1.age - profile2.age;
}
Updates the name field and stamps the modification time. 更新姓名并记录更新时间。
function profileUpdateName(profile, newName) {
profile.name = newName;
profile.updatedAt = Math.floor(Date.now() / 1000);
}
Checks whether an update timestamp exists. 检查是否存在更新时间戳。
function profileHasUpdate(profile) {
return 'updatedAt' in profile;
}
in works for own and inherited properties.in 适用于自有属性和继承属性。Converts an object into a JSON string for storage. 将对象转换为 JSON 字符串以便存储。
function profileSerialise(profile) {
return JSON.stringify(profile);
}
JSON.stringify → string.记住:JSON.stringify → 字符串。Restores JSON strings back into usable objects. 把 JSON 字符串还原为可用对象。
function profileDeserialise(profileString) {
return JSON.parse(profileString);
}
JSON.parse performs the reverse of stringify.JSON.parse 是 stringify 的逆过程。try/catch if input might be invalid.输入不可信时可配合 try/catch。// ❌ Wrong profile.updatedName = timestamp; // ✅ Correct profile.updatedAt = timestamp;
Keep naming consistent so downstream checks still work. 保持命名一致,避免后续检查失效。
// ❌ String result
age: `${currentYear - birthYear}`;
// ✅ Numeric result
age: currentYear - birthYear;
Arithmetic should return numbers, not template strings. 算术运算应返回数字,而非模板字符串。
// ❌ Milliseconds Date.now(); // ✅ Seconds Math.floor(Date.now() / 1000);
Convert to seconds whenever storing Unix time. 存储 Unix 时间时务必转换为秒。
Date.now() to Unix time.将 Date.now() 转为 Unix 秒时务必除以 1000。'prop' in object to guard optional fields.通过 'prop' in object 校验可选字段。JSON.stringify/JSON.parse round-trip.牢记 JSON.stringify/JSON.parse 的往返流程。git status until the working tree is clean before submission.提交前多次运行 git status,确保工作区干净。"Finish lab 1 practical").最后推送一次带描述的提交(如 "Finish lab 1 practical")。