 | |  |  | AIWROK软件展示JavaScript各种语句标识符的用法
- // JavaScript语句标识符详细示例
- // AIWROK软件 - 展示JavaScript各种语句标识符的用法
- // 🍎交流QQ群711841924群一,苹果内测群,528816639
- // 1. var - 声明变量
- function printl() {
- var args = Array.prototype.slice.call(arguments);
- console.log.apply(console, args);
- }
- var globalVariable = "我是全局变量";
- var numberVar = 42;
- var booleanVar = true;
- var arrayVar = [1, 2, 3, 4, 5];
- var objectVar = {name: "张三", age: 25, city: "北京"};
- function demonstrateVar() {
- var functionVariable = "我是函数内变量";
- printl("var语句示例:");
- print.log("var语句示例:");
- printl("全局变量:", globalVariable);
- print.log("全局变量:", globalVariable);
- printl("函数内变量:", functionVariable);
- print.log("函数内变量:", functionVariable);
- }
- // 2. function - 定义函数
- function calculateSum(a, b) {
- return a + b; // return语句 - 退出函数
- }
- function demonstrateFunction() {
- printl("\nfunction语句示例:");
- print.log("\nfunction语句示例:");
- var result = calculateSum(10, 20);
- printl("10 + 20 =", result);
- print.log("10 + 20 =", result);
- }
- // 3. if...else - 条件语句
- function demonstrateIfElse() {
- printl("\nif...else语句示例:");
- print.log("\nif...else语句示例:");
- var age = 20;
- var score = 85;
-
- if (age >= 18) {
- printl("成年人");
- print.log("成年人");
- } else {
- printl("未成年人");
- print.log("未成年人");
- }
-
- if (score >= 90) {
- printl("优秀");
- print.log("优秀");
- } else if (score >= 80) {
- printl("良好");
- print.log("良好");
- } else if (score >= 60) {
- printl("及格");
- print.log("及格");
- } else {
- printl("不及格");
- print.log("不及格");
- }
- }
- // 4. while - 循环语句
- function demonstrateWhile() {
- printl("\nwhile语句示例:");
- print.log("\nwhile语句示例:");
- var i = 0;
- printl("while循环输出1-5:");
- print.log("while循环输出1-5:");
- while (i < 5) {
- printl(i + 1);
- print.log(i + 1);
- i++;
- }
- }
- // 5. do...while - 循环语句
- function demonstrateDoWhile() {
- printl("\ndo...while语句示例:");
- print.log("\ndo...while语句示例:");
- var j = 0;
- printl("do...while循环输出1-3:");
- print.log("do...while循环输出1-3:");
- do {
- printl(j + 1);
- print.log(j + 1);
- j++;
- } while (j < 3);
- }
- // 6. for - 循环语句
- function demonstrateFor() {
- printl("\nfor语句示例:");
- print.log("\nfor语句示例:");
- printl("for循环输出数组元素:");
- print.log("for循环输出数组元素:");
- for (var i = 0; i < arrayVar.length; i++) {
- printl("数组第", i + 1, "个元素:", arrayVar[i]);
- print.log("数组第", i + 1, "个元素:", arrayVar[i]);
- }
- }
- // 7. for...in - 遍历对象属性
- function demonstrateForIn() {
- printl("\nfor...in语句示例:");
- print.log("\nfor...in语句示例:");
- printl("遍历对象属性:");
- print.log("遍历对象属性:");
- for (var key in objectVar) {
- printl(key + ":", objectVar[key]);
- print.log(key + ":", objectVar[key]);
- }
- }
- // 8. break - 跳出循环
- function demonstrateBreak() {
- printl("\nbreak语句示例:");
- print.log("\nbreak语句示例:");
- printl("遇到3时跳出循环:");
- print.log("遇到3时跳出循环:");
- for (var i = 1; i <= 5; i++) {
- if (i === 3) {
- printl("遇到数字3,跳出循环");
- print.log("遇到数字3,跳出循环");
- break; // 跳出循环
- }
- printl("当前数字:", i);
- print.log("当前数字:", i);
- }
- }
- // 9. continue - 跳过当前迭代
- function demonstrateContinue() {
- printl("\ncontinue语句示例:");
- print.log("\ncontinue语句示例:");
- printl("跳过偶数,只输出奇数:");
- print.log("跳过偶数,只输出奇数:");
- for (var i = 1; i <= 5; i++) {
- if (i % 2 === 0) {
- printl("跳过偶数:", i);
- print.log("跳过偶数:", i);
- continue; // 跳过当前迭代
- }
- printl("奇数:", i);
- print.log("奇数:", i);
- }
- }
- // 10. switch - 多重选择
- function demonstrateSwitch() {
- printl("\nswitch语句示例:");
- print.log("\nswitch语句示例:");
- var day = 3;
- var dayName;
-
- switch (day) {
- case 1:
- dayName = "星期一";
- break;
- case 2:
- dayName = "星期二";
- break;
- case 3:
- dayName = "星期三";
- break;
- case 4:
- dayName = "星期四";
- break;
- case 5:
- dayName = "星期五";
- break;
- case 6:
- case 7:
- dayName = "周末";
- break;
- default:
- dayName = "未知";
- }
-
- printl("今天是:", dayName);
- print.log("今天是:", dayName);
- }
- // 11. try...catch - 错误处理
- function demonstrateTryCatch() {
- printl("\ntry...catch语句示例:");
- print.log("\ntry...catch语句示例:");
-
- try {
- // 故意制造错误
- var undefinedVar = someUndefinedVariable;
- printl("这行不会执行");
- print.log("这行不会执行");
- } catch (error) {
- printl("捕获到错误:", error.message);
- print.log("捕获到错误:", error.message);
- } finally {
- printl("try...catch语句执行完毕");
- print.log("try...catch语句执行完毕");
- }
-
- try {
- // 正确的代码
- var result = calculateSum(5, 10);
- printl("计算成功:", result);
- print.log("计算成功:", result);
- } catch (error) {
- printl("捕获到错误:", error.message);
- print.log("捕获到错误:", error.message);
- }
- }
- // 12. throw - 抛出错误
- function demonstrateThrow() {
- printl("\nthrow语句示例:");
- print.log("\nthrow语句示例:");
-
- function validateAge(age) {
- if (age < 0) {
- throw new Error("年龄不能为负数");
- } else if (age > 150) {
- throw new Error("年龄不能超过150岁");
- } else if (typeof age !== "number") {
- throw new Error("年龄必须是数字");
- }
- return true;
- }
-
- try {
- validateAge(25);
- printl("年龄验证通过: 25");
- print.log("年龄验证通过: 25");
- } catch (error) {
- printl("验证失败:", error.message);
- print.log("验证失败:", error.message);
- }
-
- try {
- validateAge(-5);
- printl("这行不会执行");
- print.log("这行不会执行");
- } catch (error) {
- printl("验证失败:", error.message);
- print.log("验证失败:", error.message);
- }
- }
- // 员工绩效评估系统 - 综合示例
- function employeePerformanceEvaluation() {
- printl("\n=== 员工绩效评估系统 ===");
- print.log("\n=== 员工绩效评估系统 ===");
-
- var employees = [
- {name: "张三", score: 95, projects: 8, teamwork: 90},
- {name: "李四", score: 78, projects: 5, teamwork: 85},
- {name: "王五", score: 45, projects: 2, teamwork: 40},
- {name: "赵六", score: 88, projects: 7, teamwork: 92}
- ];
-
- var totalEmployees = employees.length;
- var excellentCount = 0;
- var goodCount = 0;
- var averageCount = 0;
- var poorCount = 0;
-
- // 使用for循环遍历员工
- for (var i = 0; i < totalEmployees; i++) {
- var employee = employees[i];
- var performance;
-
- // 使用if...else判断绩效等级
- if (employee.score >= 90 && employee.projects >= 6) {
- performance = "优秀";
- excellentCount++;
- } else if (employee.score >= 80) {
- performance = "良好";
- goodCount++;
- } else if (employee.score >= 60) {
- performance = "合格";
- averageCount++;
- } else {
- performance = "需要改进";
- poorCount++;
- }
-
- printl(employee.name + " - 绩效:" + performance +
- " (得分:" + employee.score + ", 项目:" + employee.projects +
- ", 团队合作:" + employee.teamwork + ")");
- print.log(employee.name + " - 绩效:" + performance +
- " (得分:" + employee.score + ", 项目:" + employee.projects +
- ", 团队合作:" + employee.teamwork + ")");
-
- // 使用continue跳过特定条件的员工
- if (employee.projects < 3) {
- printl(" -> 项目经验不足,跳过详细评估");
- print.log(" -> 项目经验不足,跳过详细评估");
- continue;
- }
-
- // 使用switch进行详细评估
- switch (performance) {
- case "优秀":
- printl(" -> 建议晋升或加薪");
- print.log(" -> 建议晋升或加薪");
- printl(" -> 承担更多项目领导责任");
- print.log(" -> 承担更多项目领导责任");
- break;
- case "良好":
- printl(" -> 继续当前工作表现");
- print.log(" -> 继续当前工作表现");
- printl(" -> 可考虑技能培训");
- print.log(" -> 可考虑技能培训");
- break;
- case "合格":
- printl(" -> 需要改进工作质量");
- print.log(" -> 需要改进工作质量");
- printl(" -> 建议参加技能提升课程");
- print.log(" -> 建议参加技能提升课程");
- break;
- case "需要改进":
- printl(" -> 需要制定改进计划");
- print.log(" -> 需要制定改进计划");
- printl(" -> 考虑岗位调整");
- print.log(" -> 考虑岗位调整");
- break;
- }
- }
-
- // 使用while循环进行统计
- printl("\n绩效统计:");
- print.log("\n绩效统计:");
- var categories = ["优秀", "良好", "合格", "需要改进"];
- var counts = [excellentCount, goodCount, averageCount, poorCount];
- var k = 0;
-
- while (k < categories.length) {
- printl(categories[k] + ":" + counts[k] + "人");
- print.log(categories[k] + ":" + counts[k] + "人");
- k++;
- }
-
- // 使用do...while计算平均分
- var sum = 0;
- var l = 0;
- do {
- sum += employees[l].score;
- l++;
- } while (l < employees.length);
-
- var average = sum / employees.length;
- printl("\n平均绩效得分:" + average.toFixed(2));
- print.log("\n平均绩效得分:" + average.toFixed(2));
- }
- // 空格和码行折行示例
- function demonstrateSpacingAndLineBreaks() {
- printl("\n=== 空格和码行折行示例 ===");
- print.log("\n=== 空格和码行折行示例 ===");
-
- // 正确的空格使用
- var person1 = "runoob";
- var person2 = "runoob"; // 这两种写法都正确
-
- printl("空格使用示例:");
- print.log("空格使用示例:");
- printl("person1:", person1);
- print.log("person1:", person1);
- printl("person2:", person2);
- print.log("person2:", person2);
-
- // 码行折行 - 使用反斜杠
- printl("码行折行示例:");
- print.log("码行折行示例:");
- printl("你好 \
- 世界!");
- print.log("你好 \
- 世界!");
-
- // 更好的折行方式(推荐)
- printl("推荐的多行字符串写法:");
- print.log("推荐的多行字符串写法:");
- printl("你好" +
- "世界!" +
- "欢迎学习JavaScript");
- print.log("你好" +
- "世界!" +
- "欢迎学习JavaScript");
- }
- // 代码注释示例
- function demonstrateComments() {
- printl("\n=== 代码注释示例 ===");
- print.log("\n=== 代码注释示例 ===");
-
- // 单行注释 - 这是单行注释
- var singleComment = "这是单行注释示例";
- printl(singleComment);
- print.log(singleComment);
-
- /*
- * 多行注释示例
- * 这里可以写很多注释内容
- * 用于说明复杂的代码逻辑
- * AIWROK软件
- */
- var multiLineComment = "这是多行注释示例";
- printl(multiLineComment);
- print.log(multiLineComment);
-
- /**
- * JSDoc风格的多行注释
- * @param {number} a 第一个数字
- * @param {number} b 第二个数字
- * @returns {number} 两个数字的和
- */
- function addNumbers(a, b) {
- return a + b;
- }
-
- var sum = addNumbers(5, 10);
- printl("函数注释示例 - 5 + 10 =", sum);
- print.log("函数注释示例 - 5 + 10 =", sum);
- }
- // 主函数 - 演示所有语句标识符
- function demonstrateAllStatements() {
- printl("=== JavaScript语句标识符完整示例 ===");
- print.log("=== JavaScript语句标识符完整示例 ===");
- printl("AIWROK软件 - 深入理解JavaScript语句标识符\n");
- print.log("AIWROK软件 - 深入理解JavaScript语句标识符\n");
-
- demonstrateVar();
- demonstrateFunction();
- demonstrateIfElse();
- demonstrateWhile();
- demonstrateDoWhile();
- demonstrateFor();
- demonstrateForIn();
- demonstrateBreak();
- demonstrateContinue();
- demonstrateSwitch();
- demonstrateTryCatch();
- demonstrateThrow();
- employeePerformanceEvaluation();
- demonstrateSpacingAndLineBreaks();
- demonstrateComments();
- }
- // 执行所有演示
- demonstrateAllStatements();
复制代码
| |  | |  |
|