当前位置: 首页 > 工具软件 > v-page > 使用案例 >

v-model 三元运算符_学习三元运算符-技巧与窍门

陶宏浚
2023-12-01

v-model 三元运算符

I started using ternary operator logic about six months ago and notice myself using shorthand if/else logic all the time. It shortens my code, the time to write it, and makes me look smarter to the mustaches.

大约六个月前,我开始使用三元运算符逻辑,并且发现自己一直在使用简写if / else逻辑。 它缩短了我的代码,缩短了编写代码的时间,并使我对胡须看起来更聪明。

Thanks to Google Analytics, I've found that I receive many page views from programmers looking for information on "shorthand if/else", "ternary logic", and "shorthand logic php". I've created a few guidelines for "?:" rookies to make learning shorthand if/else as quick and easy as it should be.

多亏了Google Analytics(分析),我发现我从程序员那里获得了许多页面视图,他们在寻找有关“简写if / else”,“三元逻辑”和“简写逻辑php”的信息。 我为“?:”菜鸟创建了一些准则,以使速记学习(如果/不应该)尽可能快和容易。

以If / Else开头,然后转换为三元 (Start With If/Else, Then Convert To Ternary)

Start with your expressions in simple if/else code, and then carefully convert each if/else into a shorter ternary statement. It may help to take the additional step of creating variables for each expression.

从简单的if / else代码开始,然后小心地将每个if / else转换为较短的三元语句。 采取其他步骤为每个表达式创建变量可能会有所帮助。

/* start with if / else ... */
if($language == 'php')
{
	$dynamic = true;
}
else
{
	$dynamic = false;
}

/* ... then convert */
$dynamic = ($language == 'php' ? true : false); //or 1 : 0

/* optional code shortening */
$dynamic = $language == 'php';

使用括号对逻辑进行分组 (Use Parenthesis To Group Logic)

Keeping your expressions in parentheses is a great way to keep your code organized for later maintenance.

将表达式放在括号中是一种使代码井井有条以便以后维护的好方法。

//viva grouping!
$age_code = ($age > 10 ? ($age > 20 ? ($age > 30 ? 'senior' : 'adult') : 'teen') : 'youngster');

//the following isn't as fun to read
$age_code = $age > 10 ? $age > 20 ? $age > 30 ? 'senior' : 'adult' : 'teen' : 'youngster';

在表达式的某些部分使用“中间”变量 (Use "Intermediate" Variables For Parts of the Expression)

The above 10/20/30 code is rough and can be difficult to maintain. Using variables may help simplify things.

上面的10/20/30代码很粗糙,可能很难维护。 使用变量可能有助于简化操作。

//better?
$over_30 = ($age > 30 ? 'senior' : 'adult');
$over_20 = ($age > 20 ? $over_30 : 'teen');
$age_code = ($age > 10 ? $over_20 : 'youngster');

使用True / False布尔值,而不仅仅是表达式 (Use True/False Boolean, Not Just The Expression)

As you probably know, you can use JUST the expression as the return value. If you believe that will hurt you during the learning process, explicitly return true or false.

您可能知道,您可以使用表达式作为返回值。 如果您认为在学习过程中会受到伤害,请明确返回true或false 。

/* explicit */
$can_drive = ($age >= 16 ? true : false);

/* implicit, just the expression */
$can_drive = $age >= 16;

知道何时不使用三元逻辑 (Know When Not To Use Ternary Logic)

If there are many nested if/else statements in the logic, shorthand expressions may not be the best option. For example, the following snippet of code returns whether a given year is a leap year:

如果逻辑中有许多嵌套的if / else语句,则速记表达式可能不是最佳选择。 例如,以下代码段返回给定年份是否为a年:

$is_leap_year = ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %400) == 0)));

The above code works well for ternary logic because it wont have to be updated frequently -- the leap year "calculation" is always the same. If you have code that needs to be updated often, shorthand if/else may not be the optimal choice.

上面的代码对于三元逻辑非常有效,因为它不必经常更新-year年“计算”始终相同。 如果您的代码需要经常更新,那么简写if / else可能不是最佳选择。

测试! 测试! 测试! (Test! Test! Test!)

As with any type of programming, test early and often!

与任何类型的编程一样,请经常进行早期测试!

翻译自: https://davidwalsh.name/learning-ternary-operators-tips-tricks

v-model 三元运算符

 类似资料: