Reflect,反射,JavaScript中的内置对象;反射是在程序运行中获取和动态操作自身内容的一项技术;
序号 | Reflect API | 含义 | 类似于 |
---|---|---|---|
1 | () | 读取对象属性 | obj.key 或 obj["key"] |
2 | Reflect.set() | 在对象上设置属性 | obj.key = value 或 obj["key"] = value |
3 | Reflect.has() | 判断对象是否拥有某个属性 | in 操作符 |
4 | Reflect.deleteProperty() | 删除对象的属性 | delete 操作符 |
5 | struct() | 调用构造函数创建实例对象 | new 操作符 |
6 | PrototypeOf() | 返回对象的原型 | PrototypeOf() |
7 | Reflect.setPrototypeOf() | 设置对象的原型 | Object.setPrototypeOf() |
8 | | 返回属性描述符 | |
9 | Reflect.defineProperty() | 精确添加或修改对象的属性 | Object.defineProperty() |
10 | Reflect.apply() | 调用目标函数的同时指定this和参数列表 | Function.prototype.apply() |
11 | Reflect.isExtensible() | 判断一个对象是否可扩展; | Object.isExtensible() |
12 | Reflect.preventExtensions() | 阻止新属性添加到对象(阻止扩展); | Object.preventExtensions() |
13 | Reflect.ownKeys() | 返回自身属性键组成的数组; | |
(1)语法
< ( target, key, [,receiver] )
该方法用于查找或者获取对象的属性,类似于【target[key]或target.key】;
该方法接收三个参数:
该方法返回读取目标对象的属性值;
(2)试一试
// 1. 使用()获取对象属性;
let obj = {name: "zyl",age: 18,get info() {return `info--> 姓名: ${this.name} ; 年龄 : ${this.age}`},
};console.(obj, "name")); // zyl
console.(obj, "age")); // 18
console.(obj, "phone")); // undefined
console.(obj, "info")); // info--> 姓名: zyl ; 年龄 : 18// 2. 传入receiver对象
let newObj = {name:'ll',age:20
}
console.(obj, "info", newObj)); // info--> 姓名: ll ; 年龄 : 20// 3. 传入的target不是对象
// console.(1, "name")); // called on non-object"
// console.(false, "name")); // called on non-object"
console.('str', "name")); // called on non-object"
(3)注意
(1)语法
Reflect.set ( target, key, value, [,receiver] )
该方法用于给目标对象添加属性,类似于【target.key = value 或 targe["key"] = value】;
该方法接收四个参数:
该方法返回一个布尔值,表明属性添加是否成功;
(2)试一试
// 设置对象的属性
let obj = {};
let res = Reflect.set(obj, "name", "zyl");
console.log(res); // true
console.log(obj); // {name: 'zyl'}// 改变this指向
let objNew = {};
let res1 = Reflect.set(obj, "name", "ll", objNew);
console.log(res1); // true
console.log(obj); // {name: 'zyl'}
console.log(objNew); // {name: 'll'}// 设置数组的值
let arr = [11];
let res2 = Reflect.set(arr, 1, 22);
console.log(res2); // true
console.log(arr); // [11, 22]// 截断数组
let res3 = Reflect.set(arr, "length", 1);
console.log(res3); // true
console.log(arr); // [11]// 传入的target非对象
let res4 = Reflect.set('11', 'abc','123') // 报错:Reflect.set called on non-object"
(3)异常
如果target不是Object,则会抛出TypeError异常;
(1)语法
Reflect.has ( target, key )
该方法用于判断target对象中是否具有key属性, 类似于【key in target】操作;
该方法接收两个参数:
该方法返回一个布尔值,表示对象中是否包含该属性;
(2)试一试
// 判断target中是否存在key属性
let obj = {name:'zyl'
}
console.log(Reflect.has(obj, 'name')); // true
console.log(Reflect.has(obj, 'age')); // false
// console.log(Reflect.has(1, 'age')); // 报错:“Reflect.has called on non-object”// 相当于 in 操作符
console.log('name' in obj); // true
console.log('age' in obj); // false
(3)异常
如果target不是Object,则会抛出TypeError异常;
(1)语法
Reflect.deleteProperty ( target, key )
该方法用于删除目标对象中的属性,类似于【delete target.key】操作;
该方法接收两个参数:
该方法返回一个布尔值,表示删除是否成功;
(2)试一试
let obj = {name:'zyl',phone:'111111111'
}
console.log(Reflect.deleteProperty(obj, 'name')); // true
console.log(Reflect.deleteProperty(obj, 'age')); // true
console.log(obj); // {phone: '111111111'}
// console.log(Reflect.deleteProperty(true, 'age')); // 报错:“Reflect.deleteProperty called on non-object”// 相当于delete 操作符
console.log(delete obj.phone); // true
console.log(obj); // {}
(3)异常
如果target不是Object,则会抛出TypeError异常;
(1)语法
该方法用于创建一个对象实例,类似于【new Function】实例化操作;
该方法接收三个参数:
该方法返回一份实例对象(以target为构造函数,以args为初始化参数;若newTarget存在,则以newTarget为构造函数);
(2)试一试
function Person(name, age) {this.name = name;this.age = age;
}let p1 = struct(Person, ['ace', 20]);
console.log(p1); // Person {name: 'zyl', age: 18}// 相当于new运算符
let p2 = new Person('zyl', 18);
console.log(p2); // Person {name: 'ace', age: 20}// 指定新创建对象的构造函数
function newPerson(name, age){this.name = '姓名:' + name;this.age = '年龄:'+ age;
}
let p3 = struct(Person, ['dingdong', 20], newPerson);
console.log(p3); // newPerson {name: 'dingdong', age: 20}
(3)异常
如果 target 或者 newTarget 不是构造函数,则会抛出TypeError异常;
(1)语法
该方法用于获取目标对象的原型(__proto__),类似于【
该方法接收一个参数:
该方法返回目标对象的原型;
(2)试一试
function Person(name, age) {this.name = name;this.age = age;
}
let p1 = struct(Person, ["zyl", 18]);
console.PrototypeOf(p1) === Person.prototype); // true// 对应PrototypeOf(obj)
console.PrototypeOf(p1) === PrototypeOf(p1)); // true// 若target不是对象类型PrototypeOf() 报错;而PrototypeOf() 会强制转换成对象,再执行;
console.PrototypeOf("abc")); // String {'', constructor: ƒ, anchor: ƒ, at: ƒ, big: ƒ, …}
console.PrototypeOf("abc")); // 报错:“ PrototypeOf called on non-object ”
(3)异常
如果target不是Object,则会抛出TypeError异常;
(1)语法
Reflect.setPrototypeOf(target, newProto)
该方法用于设置目标对象的原型(__proto__) ,对应【Object.setPrototypeOf(target, newProto)】;
该方法接收两个参数:
该方法返回一个Boolean值,表示给目标对象设置原型是否成功;
(2)试一试
function Person(name, age) {this.name = name;this.age = age;
}function NewPerson(name, age) {this.name = name;this.age = age;
}let p1 = struct(Person, ["zyl", 18]);
console.log(p1); // Person {name: 'zyl', age: 18}
console.log(Reflect.setPrototypeOf(p1, NewPerson)); // true// console.log(Reflect.setPrototypeOf('123', NewPerson)); // "TypeError: Reflect.setPrototypeOf called on non-object"
// console.log(Reflect.setPrototypeOf(p1, '123')); // "TypeError: Object prototype may only be an Object or null: 123"
console.log(Reflect.setPrototypeOf(p1, null)); // true
(3)异常
若目标对象target不是Object,或newProto既不是对象也不是null,则会抛出TypeError异常;
(1)语法
Reflect.defineProperty(target, key, attributes)
该方法用于给目标对象定义或修改属性描述符,类似于【Object.defineProperty()】;
该方法接收三个参数:
该方法返回一个Boolean值,表示定义或修改是否成功;
(2)试一试
let obj = {};console.log(Reflect.defineProperty(obj, "name", { value: "zyl" })); // trueconsole.log(Reflect.defineProperty(obj, "age", {value: 18,configurable: true,writable: true,enumerable: true,})
); // trueconsole.log(obj); // {age: 18, name: 'zyl'}console.log(Reflect.defineProperty('obj', "name", { value: "zyl" })); // "TypeError: Reflect.defineProperty called on non-object"
(3)异常
若target不是Object,则抛出异常TypeError异常;
(1)语法
该方法用于获取目标对象给定属性的属性描述符,类似于【
该方法接收两个参数:
如果目标对象中包含该属性,则返回其属性描述符;否则,返回undefined;
(2)试一试
let obj = {};Reflect.defineProperty(obj, "name", {value: "zyl",writable: true,enumerable: true,configurable: true,
});console.OwnPropertyDescriptor(obj, "name")); //{value: 'zyl', writable: true, enumerable: true, configurable: true}
console.OwnPropertyDescriptor(obj, "age")); //undefined
(3)异常
若target不是Object,则抛出TypeError异常;
(1)语法
Reflect.apply(targetFunction, thisArgument, argumentsList)
该方法用于指定this对象后,执行目标函数,类似于【Function.prototype.apply.call(func, thisArg, args)】;
该方法接收三个参数:
该方法返回目标函数调用后的返回结果(指定this对象和实参列表);
(2)试一试
let person = {name: "zl",age: 18,
};function getAge(obj) {console.log(this);return `年龄是${obj.age}`;
}let newPerson = {name: "ll",age: 20,
};console.log(Reflect.apply(getAge, undefined, [person])); // undefined 年龄是18
console.log(Reflect.apply(getAge, newPerson, [person])); // {name: 'll', age: 20} 年龄是18console.log(Reflect.apply(Math.floor, undefined, [1.75])); // 1
console.log(Reflect.apply("1111", undefined, [1.75])); // "TypeError: Function.prototype.apply was called on 1111, which is a string and not a function"
(3)异常
若target函数不可调用,则抛出异常TypeError异常;
(1)语法
Reflect.isExtensible(target)
该方法用于判断目标对象是否可扩展(是否可以添加新属性),类似于【Object.isExtensible()】;
该方法接收一个参数:
该方法返回一个Boolean值,表示该是否可扩展;
(2)试一试
let obj = {};
console.log(Reflect.isExtensible(obj)); // true
obj.name = "ll";
console.log(obj); // {name: 'll'}
console.log(Reflect.preventExtensions(obj)); // true; 设置为不可扩展
console.log(Reflect.isExtensible(obj)); // false
console.log(Reflect.isExtensible("111")); // "TypeError: Reflect.isExtensible called on non-object"
(3)注意
若target不是Object,则抛出异常TypeError异常;
(1)语法
Reflect.preventExtensible(target)
该方法用于阻止给目标对象添加属性,类似于【Object.preventExtensible(target)】;
该方法接收一个参数:
该方法返回Boolean值,表明目标对象是否被成功设置为不可扩展;
(2)试一试
let obj = {};
obj.name = "zyl";
console.log(obj); // {name: 'zyl'}
console.log(Reflect.preventExtensions(obj)); // true
// console.log(Reflect.preventExtensions('111')); // "TypeError: Reflect.preventExtensions called on non-object"
console.log((obj.age = 18)); // "TypeError: Cannot add property age, object is not extensible"
(3)注意
若target不是Object,则抛出异常TypeError异常;
(1)语法
Reflect.ownKeys(target)
该方法用于获取目标对象的所有属性键组成的数组,类似于【
该方法接受一个参数:
该方法返回一个数组(目标对象所有属性键组成的数组,包括符号类型) ;
(2)试一试
const s1 = Symbol();
const s2 = Symbol();
let obj = {name: "zyl",age: 18,[s1]: "aaa",[s2]: "bbb",
};
console.log(Reflect.ownKeys(obj)); // ['name', 'age', Symbol(), Symbol()]
console.OwnPropertyNames(obj)); // ['name', 'age']
console.OwnPropertySymbols(obj)); // [Symbol(), Symbol()]let arr = [1, 2, 4, 5, 3];
console.log(Reflect.ownKeys(arr)); // ['0', '1', '2', '3', '4', 'length']console.log(Reflect.ownKeys('1111')); // "TypeError: Reflect.ownKeys called on non-object"
},
(3)异常
若target不是Object,则会抛出TypeError异常;
本文发布于:2024-02-03 23:36:21,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170697665251645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |