문제

In PHP, we can assign a variable AND test it at the same time:

<?php
if ($result = $this->find()) {
    $this->do_something($result);
}

function find() {
   if ($this->day == 'Sunday')
       return '1234';
   else
       return false;
}

In the above example, on Sundays, $result is set to '1234' and calls the do_something function. On other days, it is set to false and nothing else occurs.

Is this kind of thing possible with Javascript?

도움이 되었습니까?

해결책

Yes, it is possible.

var day=[
    'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday',
][new Date().getDay()];

if(result = find()){
    do_something(result);
}

function find(){
   if(day == 'Sunday')
       return '1234';
   else
       return false;
}

Tada! Calls do_something on Sundays.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top