Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top