shorthand

Shorthand if/else statement Javascript

天涯浪子 提交于 2019-11-27 05:15:19
问题 I'm wondering if there's a shorter way to write this: var x = 1; if(y != undefined) x = y; I initially tried x = y || 1 , but that didn't work. What's the correct way to go about this? 回答1: var x = y !== undefined ? y : 1; Note that var x = y || 1; would assign 1 for any case where y is falsy (e.g. false , 0 , "" ), which may be why it "didn't work" for you. Also, if y is a global variable, if it's truly not defined you may run into an error unless you access it as window.y . As vol7ron

Constructor behaving differently using ES6 shorthand notation

馋奶兔 提交于 2019-11-26 23:09:17
问题 ES6 introduced a shorthand notation to initialize objects with functions and properties. // ES6 shorthand notation const obj1 = { a(b) { console.log("ES6: obj1"); } }; // ES5 var obj2 = { a: function a(b) { console.log("ES5: obj2"); } }; obj2.a(); obj1.a(); new obj2.a(); new obj1.a(); However, these different notations behave differently, as you can see. If I do new obj1.a() in the browser (tested Chrome and Firefox), I get a TypeError: obj1.a is not a constructor . new obj2.a() behaves

Difference in C# between different getter styles

限于喜欢 提交于 2019-11-26 19:39:06
问题 I do sometimes see abbreviations in properties for the getter. E.g. those two types: public int Number { get; } = 0 public int Number => 0; Can someone please tell me if there are any differences between those two. How do they behave? Are both of them read-only? 回答1: Yes, both of them are read-only, but there is a difference. In the first one, there's a backing field which is initialized to 0 before the constructor is executed. You can change the value only in the constructor , just like a

PHP shorthand for isset()? [duplicate]

元气小坏坏 提交于 2019-11-26 17:59:07
问题 This question already has answers here : What is the PHP shorthand for: print var if var exist (11 answers) Closed 6 years ago . Is there a shorthand way to assign a variable to something if it doesn't exist in PHP? if(!isset($var) { $var = ""; } I'd like to do something like $var = $var | ""; 回答1: Update for PHP 7 (thanks shock_gone_wild) PHP 7 introduces the so called null coalescing operator which simplifies the below statements to: $var = $var ?? "default"; Before PHP 7 No, there is no

Which “if” construct is faster - statement or ternary operator?

百般思念 提交于 2019-11-26 15:36:17
问题 There are two types of if statements in java - classic: if {} else {} and shorthand: exp ? value1 : value2 . Is one faster than the other or are they the same? statement: int x; if (expression) { x = 1; } else { x = 2; } ternary operator: int x = (expression) ? 1 : 2; 回答1: There's only one type of "if" statement there. The other is a conditional expression. As to which will perform better: they could compile to the same bytecode, and I would expect them to behave identically - or so close

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

梦想的初衷 提交于 2019-11-26 15:33: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. in_array() is what I use if (in_array($variable, array('one','two','three'))) { Without the need of constructing an array: if (strstr('onetwothree', $variable)) /

CSS transition shorthand with multiple properties?

拟墨画扇 提交于 2019-11-26 14:51:26
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 transition. Testing in latest Chrome, FF and Safari. What am I doing wrong? EDIT: Just to be clear, I'm

javascript shorthand if statement, without the else portion

こ雲淡風輕ζ 提交于 2019-11-26 13:45:31
问题 So I'm using a shorthand javascript if / else statement (I read somewhere they're called Ternary statements?) this.dragHandle.hasClass('handle-low') ? direction = "left" : direction = "right" This works great, but what if later I want to use just a shorthand if , without the else portion. Like: direction == "right" ? slideOffset += $(".range-slide").width() is this possible at all? 回答1: you can use && operator - second operand expression is executed only if first is true direction == "right"

$(document).ready shorthand

一曲冷凌霜 提交于 2019-11-26 11:32:57
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. 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 is an immediately-invoked function expression (IIFE) with the jQuery object as its argument. Its purpose is

What exactly does += do in python?

我们两清 提交于 2019-11-26 10:14:54
I need to know what += does in python. It's that simple. I also would appreciate links to definitions of other short hand tools in python. Bryan In Python, += is sugar coating for the __iadd__ special method, or __add__ or __radd__ if __iadd__ isn't present. The __iadd__ method of a class can do anything it wants. The list object implements it and uses it to iterate over an iterable object appending each element to itself in the same way that the list's extend method does. Here's a simple custom class that implements the __iadd__ special method. You initialize the object with an int, then can