admin管理员组

文章数量:1430551

Basically if a variable isn't set then set it to another value.

There must be a better way, this looks so messy.

        if ($image_src === undefined) {
                $image_src = $apple_icon;
            }

            if ($image_src === undefined) {
                $image_src = $apple_icon2;
            }

            if ($image_src === undefined) {
                $image_src = $item_prop_image;
            }

            if ($image_src === undefined) {
                $image_src = $image_first;
            }

Basically if a variable isn't set then set it to another value.

There must be a better way, this looks so messy.

        if ($image_src === undefined) {
                $image_src = $apple_icon;
            }

            if ($image_src === undefined) {
                $image_src = $apple_icon2;
            }

            if ($image_src === undefined) {
                $image_src = $item_prop_image;
            }

            if ($image_src === undefined) {
                $image_src = $image_first;
            }
Share Improve this question edited Nov 7, 2012 at 22:32 chris asked Nov 7, 2012 at 22:12 chrischris 6051 gold badge9 silver badges27 bronze badges 5
  • 5 It is syntactically correct, though i question the logic behind it. – Kevin B Commented Nov 7, 2012 at 22:16
  • This does seem kind of messy. Some times, when confronted with something that requires you to write "ugly" code (no offence ;), you should take a step back and look at your application's flow. Perhaps you need to alter the logic slightly to make your code more maintainable (and readable) – Lix Commented Nov 7, 2012 at 22:17
  • 1 @chris Is there a possibility that the $apple_icon variable itself (or any of the rest) is not defined? If so, then your script will actually blow up, not just return undefined. You'd need to check with something like if typeof($apple_icon !== "undefined") – Joe Enos Commented Nov 7, 2012 at 22:21
  • @JoeEnos No, I don't think so.. I think he has that many if just because $apple_icon may be undefined. – Selvakumar Arumugam Commented Nov 7, 2012 at 22:22
  • @KevinB: Questioning the logic behind things is a path to the Dark Side – Jens Roland Commented Nov 7, 2012 at 22:39
Add a ment  | 

4 Answers 4

Reset to default 10

In JavaScript you can use the or || operator to condense things that are undefined. So this is valid:

$image_src = $image_src || $apple_icon || $apple_icon1;
$image_src = $image_src || $apple_icon;

http://billhiggins.us/blog/2007/02/13/the-javascript-logical-or-assignment-idiom/

Expanding on my ment - just in case you do have the scenario where one of the variables may not have been declared, you can do something like the following - harder to read, but safer:

$image_src = getWhateverTheInitialValueIsSupposedToBe();

$image_src = $image_src || (
    (typeof($apple_icon) !== "undefined" && $apple_icon) ? $apple_icon :
    (typeof($apple_icon2) !== "undefined" && $apple_icon2) ? $apple_icon2 :
    (typeof($item_prop_image) !== "undefined" && $item_prop_image) ? $item_prop_image :
    (typeof($image_first) !== "undefined" && $image_first) ? $image_first :
    $image_src);

Honestly, I think the way you've written it shows clearly what you want to do.

You can make it more pact through the ways shown in the other answers, but I find the way you wrote it is more readily understandable at first glance than the others IMO.

It all depends on what you want. The || method is probably more efficient, but yours is quite readable.

Wrapping this in a function would be fine.

本文标签: jqueryIs this javascript syntactically correct or is there a better wayStack Overflow