外观
外观
耶温
1089字约4分钟
2024-08-12
ECMAScript(简称 ES)是一种由 ECMA 国际组织制定的脚本语言标准。它是 JavaScript 的基础,定义了语言的语法、类型、语句、关键字、保留字、操作符、内置对象等。ECMAScript 的目标是提供一种通用的脚本语言,使得不同的实现(如浏览器、服务器等)能够遵循相同的标准,从而实现跨平台的兼容性。
Object.create、Object.defineProperty 等方法严格模式(Strict Mode)是 ECMAScript 5(ES5)引入的一种在 JavaScript 中运行的更严格的解析和执行模式。通过启用严格模式,开发者可以编写更安全、更高效的代码。严格模式的主要目的是消除 JavaScript 中一些不良的语言特性,避免潜在的错误,并提高代码的可维护性。
"use strict";
function myFunction() {
// 在这个函数中启用严格模式
}"use strict";
x = 10; // ReferenceError: x is not defined"use strict";
var x = 10;
delete x; // SyntaxError: Delete of an unqualified identifier in strict mode."use strict";
this.x = 10; // TypeError: Cannot set property 'x' of #<Object>"use strict";
var x = 10;
var x = 20; // SyntaxError: Identifier 'x' has already been declaredwith 语句"use strict";
with (obj) {
x = 10;
} // SyntaxError: Strict mode code may not include a with statementeval() 函数"use strict";
eval("var x = 10"); // SyntaxError: Strict mode code may not include eval()arguments.callee 和 arguments.caller"use strict";
function myFunction() {
arguments.callee(); // SyntaxError: Strict mode code may not include arguments.callee
arguments.caller; // SyntaxError: Strict mode code may not include arguments.caller
}Array.prototype.forEach(callback):对数组的每个元素执行一次提供的函数。
const arr = [1, 2, 3];
arr.forEach((element) => {
console.log(element); // 输出 1, 2, 3
});Array.prototype.map(callback):创建一个新数组,包含调用提供的函数处理每个元素后的结果。
const arr = [1, 2, 3];
const doubled = arr.map((element) => element * 2); // [2, 4, 6]Array.prototype.filter(callback):创建一个新数组,包含所有通过测试的元素。
const arr = [1, 2, 3, 4];
const evens = arr.filter((element) => element % 2 === 0); // [2, 4]Array.prototype.reduce(callback, initialValue):对数组中的每个元素执行一个 reducer 函数,最终计算出一个单一的值。
const arr = [1, 2, 3];
const sum = arr.reduce((accumulator, current) => accumulator + current, 0); // 6Array.prototype.every(callback):测试数组中的所有元素是否都满足提供的测试函数。
const arr = [2, 4, 6];
const allEven = arr.every((element) => element % 2 === 0); // trueArray.prototype.some(callback):测试数组中是否至少有一个元素满足提供的测试函数。
const arr = [1, 2, 3];
const hasEven = arr.some((element) => element % 2 === 0); // trueArray.prototype.indexOf(searchElement, fromIndex):返回数组中首次出现的指定元素的索引,如果未找到则返回 -1。
const arr = [1, 2, 3];
const index = arr.indexOf(2); // 1Array.prototype.lastIndexOf(searchElement, fromIndex):返回数组中最后一次出现的指定元素的索引,如果未找到则返回 -1。
const arr = [1, 2, 3, 2];
const lastIndex = arr.lastIndexOf(2); // 3Object.create(proto, propertiesObject):创建一个新对象,使用指定的原型对象和可选的属性。
const proto = { greet: function() { console.log("Hello!"); } };
const obj = Object.create(proto);
obj.greet(); // "Hello!"Object.defineProperty(obj, prop, descriptor):在对象上定义新属性或修改现有属性的特性。
const obj = {};
Object.defineProperty(obj, 'name', {
value: 'Alice',
writable: false
});
obj.name = 'Bob'; // 不会改变,因为 writable 为 falseObject.defineProperties(obj, props):在对象上定义多个新属性或修改现有属性的特性。
const obj = {};
Object.defineProperties(obj, {
'name': { value: 'Alice', writable: false },
'age': { value: 30, writable: true }
});Object.keys(obj):返回一个数组,包含对象自身的所有可枚举属性的名称。
const obj = { a: 1, b: 2 };
const keys = Object.keys(obj); // ['a', 'b']String.prototype.trim():去除字符串两端的空白字符。const str = " Hello, World! ";
const trimmed = str.trim(); // "Hello, World!"JSON.stringify(value, replacer, space):将 JavaScript 值转换为 JSON 字符串。
const obj = { name: "Alice", age: 30 };
const jsonString = JSON.stringify(obj); // '{"name":"Alice","age":30}'JSON.parse(text, reviver):将 JSON 字符串解析为 JavaScript 值。
const jsonString = '{"name":"Alice","age":30}';
const obj = JSON.parse(jsonString); // { name: "Alice", age: 30 }