Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top