一、Reflect映射
defineProperty() 参数:obj:具体的对象、attr:属性、{value}:属性值
var obj = {};
Object.defineProperty(obj, 'age', {
value:18
});
Reflect.defineProperty(obj, 'name', {
value:"张三"
});
console.log('name' in obj)
//映射进行判断属性
console.log(Reflect.has(obj, 'age'))
let obj = {
age:18
}
//proxy 阻截
new Proxy(obj, {
set:function(){
}
});
//Reflect.set(target, attr, value, self)
Reflect.set(obj, 'name', '张三')
let proxy = new Proxy(obj, {
set:function(target, attr, value, self){
Reflect.set(target, attr, value, self)
}
});
二、Reflect静态方法
var obj = {
age:18,
set index(val){
this.age = val; //this指向obj1
}
}
var obj1 = {
age:20
}
var r = Reflect.get(obj, 'age')
console.log(r)
Reflect.set(obj, 'index', 30, obj1)
console.log(obj1, obj)
var obj = {
age:18,
[Symbol()]:999
}
var obj1 = {
index:888
}
console.log(obj)
//deleteProperty 删除属性
Reflect.deleteProperty(obj, 'age')
//Reflect.setPrototypeOf(obj, obj1) 设置修改原型
console.log(obj, obj1)
/*
writable:可写的
样式可读:obj.age
enumerable:可列举的(可遍历的,可枚举)
for(), for...in, for...of
configurable
样式可写:obj.age = 99;
*/
var r = Reflect.getOwnPropertyDescriptor(obj, 'age');
console.log(r)
Reflect.defineProperty(
obj, 'index', {
value:999
}
);
console.log(obj)
for(var key in obj){
console.log(key)
}
//Reflect.ownKeys(obj) //遍历所有的属性,包括私有的属性
for(var key of Reflect.ownKeys(obj)){
console.log(key)
}
function Fn(x, y){
this.x = x;
this.y = y;
}
var obj = new Fn(10, 20)
console.log(obj)
var obj1 = Reflect.construct(Fn, [30, 40])
console.log(obj1)
//Reflect.isExtensible(target) 判断扩展
//Reflect.preventExtensions 阻止扩展
var obj = {
age:18
}
var r = Reflect.isExtensible(obj);
console.log(r)
obj.index = 666;
console.log(obj)
r = Reflect.preventExtensions(obj)
obj.name = "张三";
console.log(obj)
三、Promise
状态:pending 进行中、resolved 已解决(fulfilled 已成功、rejected 已失败)
var promise = new Promise(
function(success, error){
success("成功");
}
);
console.log(promise)
var promise1 = new Promise(
function(success, error){
error("失败了!");
}
//(success, error) => {
// error("失败了!");
//}
)
console.log(promise1);
var promise = new Promise(
//形参:第一个值:诺言成功;第二个值:诺言失败
function(success, error){
setTimeout(()=>{
error()
}, 3000);
}
);
console.log(promise)
promise.then(function(){
console.log("成功了!");
}, function(){
console.log("失败了!");
})
var promise = new Promise(function(success, error){
setTimeout(()=>{
success("已经成功了!");
//error("已经失败了");
}, 3000);
});
promise.then((message)=>{
console.log(message)
}, (message)=>{
console.log(message)
});
catch 捕获到错误的信息
promise.then(function(){
console.log("ok");
}).catch(function(){
console.log("error")
});
var promise = new Promise(function(success, error){
// catch 对throw newError的内容进行捕获
throw new Error("Uncaught SyntaxError: missing after argument list");
});
promise.then(function(){
console.log("ok")
}).catch(function(e){
console.log(e)
});
PromiseStatus 已经完成了
PromiseValue 已经完成时反馈出来的值
var promise = Promise.resolve("404");
console.log(promise)
var promise1 = new Promise((success, error) => {
success("404");
})
console.log(promise1)
var p1 = new Promise((success, error) => {
success();
})
var p2 = new Promise((success, error) => {
success();
})
var p3 = new Promise((success, error) => {
success();
})
var promise = Promise.all([p1, p2, p3]);
promise.then(()=>{
console.log("诺言全成功")
}, ()=>{
console.log("诺言有一个失败")
})
var p1 = new Promise((success, error) => {
setTimeout(()=>{
error("已经失败了");
}, 3000)
})
var p2 = new Promise((success, error) => {
setTimeout(()=>{
error("已经失败了");
}, 2000)
})
var p3 = new Promise((success, error) => {
setTimeout(()=>{
success("已经成功了");
}, 1000)
})
var promise = Promise.race([p1, p2, p3]);
promise.then(()=>{
console.log("诺言第一个成功")
}, ()=>{
console.log("诺言第一个失败")
})
网友评论