// 单行注释
/* 多行
注释 */
// 声明变量
var a = "Hello";
let b = "World";
const pi = 3.14;
// 数组
var array = [1, 2, 3, 4, 5];
// 对象
var obj = {
name: "John Doe",
age: 30,
isMarried: true,
children: ['Jane', 'Joe'],
greet: function() {
console.log("Hello, " + this.name);
}
};
obj.greet(); //调用对象中的函数
// 条件语句
if (obj.age > 20) {
console.log("John is old.");
} else {
console.log("John is young.");
}
// 循环
for (let i = 0; i < 10; i++) {
console.log(i);
}
// 函数
function add(x, y) {
return x + y;
}
console.log(add(5, 3)); // 输出 8
// 异步操作
setTimeout(function() {
console.log("This message is delayed by 3 seconds");
}, 3000);
// Promise
let testPromise = new Promise(function(resolve, reject) {
a > 10 ? resolve(a) : reject('a is less than 10');
});
testPromise
.then(number => console.log('Promise Resolved: ' + number))
.catch(err => console.log('Promise Rejected: ' + err));
// ES6 箭头函数
const multiply = (x, y) => x * y;
console.log(multiply(3, 4)); // 输出 12
网友评论