Получить значения из вложенной функции, используя это

Источник: Как написать dispatch_after GCD в Swift 3? Для этого вы можете использовать dispatch_group. Например (код ObjC):

dispatch_group_t group = dispatch_group_create();

//startOperation1
dispatch_group_enter(group);

//finishOpeartion1
dispatch_group_leave(group);


//startOperation2
dispatch_group_enter(group);

//finishOpeartion2
dispatch_group_leave(group);


//Handle both operations completion
dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 
//code here
});

0
задан Lala 29 March 2019 в 12:05
поделиться

2 ответа

function times() {
  var currentTime = function() {
    var hourMin = new Date().getHours() + ":" + new Date().getMinutes();
    return hourMin;
  };

  var pastTime = function() {
    if (new Date().getDay() == 5) {
      return "07:40"
    } else {
      return "16:30"
    }
  };
  
  return {
    present: currentTime,
    past: pastTime
  };
}

console.log(times().present())

0
ответ дан Nicolae Maties 29 March 2019 в 12:05
поделиться

Вы можете использовать метод call().

function times() {
  var timingObj = function() {
    this.present = currentTime;
    this.past = pastTime;
  };

  var currentTime = function() {
    var hourMin = new Date().getHours() + ":" + new Date().getMinutes();
    return hourMin;
  };

  var pastTime = function() {
    if (new Date().getDay() == 5) {
      return "07:40"
    } else {
      return "16:30"
    }
  };
  return timingObj;
}

times().call(null);
console.log(present(), past());

ИЛИ определяют их как prototype

function times() {
  var timingObj = function() {
    this.present = timingObj.prototype.currentTime;
    this.past = timingObj.prototype.pastTime;
  };

  timingObj.prototype.currentTime = function() {
    return new Date().getHours() + ":" + new Date().getMinutes();
  };

  timingObj.prototype.pastTime = function() {
    return new Date().getDay() === 5 ? "07:40" : "16:30";
  };

  return timingObj;
}

console.log(times().prototype.currentTime(), times().prototype.pastTime());

//times().call(null);
//console.log(present(), past());
[ 116]

0
ответ дан O.O 29 March 2019 в 12:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: