shorthand

In PHP, is there a short way to compare a variable to multiple values?

拥有回忆 提交于 2019-11-26 04:28:24
问题 Basically what I\'m wondering if there is a way to shorten something like this: if ($variable == \"one\" || $variable == \"two\" || $variable == \"three\") in such a way that the variable can be tested against or compared with multiple values without repeating the variable and operator every time. For example, something along the lines of this might help: if ($variable == \"one\" or \"two\" or \"three\") or anything that results in less typing. 回答1: in_array() is what I use if (in_array(

Omitting the second expression when using the if-else shorthand

这一生的挚爱 提交于 2019-11-26 04:04:43
问题 Can I write the if else shorthand without the else ? var x=1; x==2 ? dosomething() : doNothingButContinueCode(); I\'ve noticed putting null for the else works (but I have no idea why or if that\'s a good idea). Edit: Some of you seem bemused why I\'d bother trying this. Rest assured it\'s purely out of curiosity. I like messing around with JavaScript. 回答1: This is also an option: x==2 && dosomething(); dosomething() will only be called if x==2 is evaluated to true. This is called Short

CSS transition shorthand with multiple properties?

烈酒焚心 提交于 2019-11-26 04:02:59
问题 I can\'t seem to find the correct syntax for the CSS transition shorthand with multiple properties. This doesn\'t do anything: .element { -webkit-transition: height .5s, opacity .5s .5s; -moz-transition: height .5s, opacity .5s .5s; -ms-transition: height .5s, opacity .5s .5s; transition: height .5s, opacity .5s .5s; height: 0; opacity: 0; overflow: 0; } .element.show { height: 200px; opacity: 1; } I add the show class with javascript. The element becomes higher and visible, it just doesn\'t

$(document).ready shorthand

六眼飞鱼酱① 提交于 2019-11-26 03:32:38
问题 Is the following shorthand for $(document).ready ? (function($){ //some code })(jQuery); I see this pattern used a lot, but I\'m unable to find any reference to it. If it is shorthand for $(document).ready() , is there any particular reason it might not work? In my tests it seems to always fire before the ready event. 回答1: The shorthand for $(document).ready(handler) is $(handler) (where handler is a function). See here. The code in your question has nothing to do with .ready() . Rather, it

How does this object method definition work without the “function” keyword?

柔情痞子 提交于 2019-11-26 03:14:01
问题 I discovered this by accidentally leaving off the function keyword. Ordinarily the foobar method in the module below would be declared as foobar: function(arg1) , but interestingly the following works, at least in some browsers, e.g. Chrome Version 44.0.2403.157 m, but it fails in IE 11.0.9600.17959 How is it possible that this runs at all in any browser? Is this some sort of new ES6 functionality? var module = { foobar(arg1) { alert(arg1); } }; module.foobar(\"Hello World\"); 回答1: How is it

Multiline string literal in C#

心已入冬 提交于 2019-11-25 23:27:01
问题 Is there an easy way to create a multiline string literal in C#? Here\'s what I have now: string query = \"SELECT foo, bar\" + \" FROM table\" + \" WHERE id = 42\"; I know PHP has <<<BLOCK BLOCK; Does C# have something similar? 回答1: You can use the @ symbol in front of a string to form a verbatim string literal: string query = @"SELECT foo, bar FROM table WHERE id = 42"; You also do not have to escape special characters when you use this method, except for double quotes as shown in Jon Skeet