Promise 与 .call()函数

Promise函数写法

var test = new Promise(function(resolve,reject){
    function test1 (){
        var pOk;
        var pCatch;
        if(y){
            resolve(pOK);
        }else{
            reject(pCatch);
        }
    }
});

test.then(function(data){
    console.log(data); // Promise中返回得值
});

.call()函数

.call()函数用于改变调用函数中this的指向。被调用函数中的this指向的不是函数本身,而是调用他的函数。

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  // 调用后product中的this指向的是Food而不是Product函数本身
  Product.call(this, name, price);
  this.category = 'food';
}

console.log(new Food('cheese', 5).name);

// 结果输出 ‘cheese’

 

猜你喜欢

转载自blog.csdn.net/oneKnow/article/details/99622788
今日推荐