首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

声明和初始化的问题

代码语言:javascript
复制
//声明和初始化
function init() {
  // 初始化变量
  let name = "John Doe";
  let age = 30;
  let isStudent = false;
  let city = "New York";
  let favoriteColor = "blue";
  let favoriteFood = "pizza";
  let hobbies = ["reading", "swimming", "traveling"];

  // 初始化数组
  let array1 = [1, 2, 3];
  let array2 = ["apple", "banana", "orange"];

  // 初始化对象
  let obj1 = {
    name: "Jane Doe",
    age: 25,
    isStudent: true,
    city: "Los Angeles",
    favoriteColor: "red",
    favoriteFood: "hamburgers",
    hobbies: ["running", "yoga", "reading"],
  };

  // 初始化函数
  let myFunction = function () {
    console.log("Hello World!");
  };

  // 初始化其他东西...
  return {
    name: name,
    age: age,
    isStudent: isStudent,
    city: city,
    favoriteColor: favoriteColor,
    favoriteFood: favoriteFood,
    hobbies: hobbies,
    array1: array1,
    array2: array2,
    obj1: obj1,
    myFunction: myFunction,
  };
}

// 调用 init 函数
let initResult = init();

// 输出结果
console.log("Name:", initResult.name);
console.log("Age:", initResult.age);
console.log("isStudent:", initResult.isStudent);
console.log("City:", initResult.city);
console.log("Favorite Color:", initResult.favoriteColor);
console.log("Favorite Food:", initResult.favoriteFood);
console.log("Hobbies:", initResult.hobbies);
console.log("Array 1:", initResult.array1);
console.log("Array 2:", initResult.array2);
console.log("Object 1:", initResult.obj1);
console.log("myFunction():", initResult.myFunction());
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券