shorthand

Return result from Ternary in one line (JavaScript)

对着背影说爱祢 提交于 2021-02-17 07:22:45
问题 In JavaScript, rather than having to assign the result to a variable, is it possible to return the result of a ternary in one line of code? e.g. Instead of this: function getColor(val){ var result = val <= 20 ? '#000' : val >= 80 ? '#999' : '#555'; return result; } Can we do something like this... function getColor(val){ return val <= 20 ? '#000' : val >= 80 ? '#999' : '#555'; } I am asking this because I just tried the above and nothing was returned. 回答1: Yes. It's possible. Also you can

Shorthand conditional to define a variable based on the existence of another variable in PHP

孤街醉人 提交于 2021-02-16 05:49:32
问题 Essentially, I'd love to be able to define a variable as one thing unless that thing doesn't exist. I swear that somewhere I saw a shorthand conditional that looked something like this: $var=$_GET["var"] || "default"; But I can't find any documentation to do this right, and honestly it might have been JS or ASP or something where I saw it. I understand that all that should be happening in the above code is just to check if either statement returns true. But I thought I saw someone do

Shorthand conditional to define a variable based on the existence of another variable in PHP

为君一笑 提交于 2021-02-16 05:49:08
问题 Essentially, I'd love to be able to define a variable as one thing unless that thing doesn't exist. I swear that somewhere I saw a shorthand conditional that looked something like this: $var=$_GET["var"] || "default"; But I can't find any documentation to do this right, and honestly it might have been JS or ASP or something where I saw it. I understand that all that should be happening in the above code is just to check if either statement returns true. But I thought I saw someone do

c# shorthand for if not null then assign value

梦想与她 提交于 2021-02-15 05:59:24
问题 Is there any shorthand in c# now that will cutdown the following code: var testVar1 = checkObject(); if (testVar1 != null) { testVar2 = testVar1; } In this situation only want to assign testVar2 if testVar1 is not null from the CheckObject() result (testVar2 has a setter that will fire off code). Was trying to think how could use the null coalesce stuff but not really working out. Adding on to this testVar2 has code on it's setter to fire, so do not want testVar2 being set to anything if the

How to break a loop by a shorthand “if-else” result?

回眸只為那壹抹淺笑 提交于 2021-01-03 07:01:53
问题 Suppose I have got a shorthand if-else statement inside a loop as in this case : for(...) { a = b == c ? b : c; // More unnecessary code if the result was true. } And I would like to break the loop by the result of the condition: for(...) { a = b == c ? b break : c; // Now the unnecessary code is not executed. } I realize I could just type it in a full way as in this example: for(...) { if( b == c ) { a = b; break; } else { a = c; } // Now the unnecessary code is not executed. } But it is too

How to break a loop by a shorthand “if-else” result?

瘦欲@ 提交于 2021-01-03 07:00:57
问题 Suppose I have got a shorthand if-else statement inside a loop as in this case : for(...) { a = b == c ? b : c; // More unnecessary code if the result was true. } And I would like to break the loop by the result of the condition: for(...) { a = b == c ? b break : c; // Now the unnecessary code is not executed. } I realize I could just type it in a full way as in this example: for(...) { if( b == c ) { a = b; break; } else { a = c; } // Now the unnecessary code is not executed. } But it is too

Bash exit status of shorthand increment notation

守給你的承諾、 提交于 2020-12-25 08:42:52
问题 I noticed an apparent inconsistency in the return status of bash's (( )) notation. Consider the following $> A=0 $> ((A=A+1)) $> echo $? $A 0 1 However using the other well known shorthand increment notation yields: $> A=0 $> ((A++)) $> echo $? $A 1 1 If one has the builtin set -e in the script the second notation will cause the script to exit, since the exit status of the ((A++)) returned non-zero. This question was more or less addressed in this related question. But it does not seem to

Bash exit status of shorthand increment notation

天大地大妈咪最大 提交于 2020-12-25 08:42:08
问题 I noticed an apparent inconsistency in the return status of bash's (( )) notation. Consider the following $> A=0 $> ((A=A+1)) $> echo $? $A 0 1 However using the other well known shorthand increment notation yields: $> A=0 $> ((A++)) $> echo $? $A 1 1 If one has the builtin set -e in the script the second notation will cause the script to exit, since the exit status of the ((A++)) returned non-zero. This question was more or less addressed in this related question. But it does not seem to

Shorthand return in Go (golang)

懵懂的女人 提交于 2020-01-11 13:57:07
问题 The following code generates a syntax error ( unexpected ++ at end of statement ) in Go 1.6 or 1.7: package main import "fmt" var x int func increment() int { return x++ // not allowed } func main() { fmt.Println( increment() ) } Shouldn't this be permitted? 回答1: It's an error, because the ++ and -- in Go are statements, not expressions: Spec: IncDec Statements (and statements have no results that would be returned). For reasoning, see Go FAQ: Why are ++ and -- statements and not expressions?

An explanation of && shorthand in JavaScript

限于喜欢 提交于 2020-01-03 05:24:07
问题 Using a watermark plugin for jQuery, I'm attempting to jslint and minimize the functions but I've come across syntax I have never seen before wherein there are expressions where there really ought to be an assignment or function call: (function($) { $.fn.watermark = function(css, text) { return this.each(function() { var i = $(this), w; i.focus(function() { w && !(w=0) && i.removeClass(css).data('w',0).val(''); }) .blur(function() { !i.val() && (w=1) && i.addClass(css).data('w',1).val(text);