Promise and .call() function

Promise function writing

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() function

The .call() function is used to change the point of this in the calling function. This in the called function does not point to the function itself, but the function that calls him.

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’

 

Guess you like

Origin blog.csdn.net/oneKnow/article/details/99622788