在JavaScript中,有许多常用的对象,这些对象提供了各种功能和操作。以下是一些常见的JavaScript对象及其简要描述:
length
, charAt()
, concat()
, indexOf()
, split()
, substring()
等。toFixed()
, toExponential()
, toPrecision()
等。push()
, pop()
, shift()
, unshift()
, splice()
, slice()
, map()
, filter()
, reduce()
等。keys()
, values()
, entries()
, assign()
等。getFullYear()
, getMonth()
, getDate()
, getDay()
, getTime()
等。PI
, sqrt()
, pow()
, random()
, round()
等。test()
, exec()
等。call()
, apply()
, bind()
等。document
, location
, history
, alert()
, confirm()
, prompt()
等。body
, head
, getElementById()
, querySelector()
, createElement()
等。type
, target
, preventDefault()
, stopPropagation()
等。then()
, catch()
, finally()
等。set()
, get()
, has()
, delete()
, clear()
等。add()
, delete()
, has()
, clear()
等。parse()
, stringify()
等。以下是一个简单的示例,展示了如何使用一些常见的JavaScript对象和方法:
// String
let str = "Hello, World!";
console.log(str.length); // 13
console.log(str.charAt(0)); // H
// Array
let arr = [1, 2, 3, 4, 5];
arr.push(6);
console.log(arr); // [1, 2, 3, 4, 5, 6]
// Date
let now = new Date();
console.log(now.getFullYear()); // 当前年份
// Math
console.log(Math.random()); // 随机数
// Object
let obj = { name: "Alice", age: 25 };
console.log(obj.name); // Alice
// Promise
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Done!"), 1000);
});
promise.then(message => console.log(message)); // Done!
这些对象和方法只是JavaScript中常用的一部分,掌握它们可以帮助你更有效地进行前端开发和后端开发。
领取专属 10元无门槛券
手把手带您无忧上云