let gender = user.male ? "male" : "female";
let displayName = user.name ?: "";
let displayName = user.name ?. "";
let displayName = user.name || "";
Above, all operators in the same way perform the same process. What are the advantages of the difference, and which are better and worse?
, , , , TypeScript elvis . , , !., , js ! . Angular , ||. - .
!.
!
||
, TypeScript ?:, , ,
?:
vs . , 3 . , "", .
, 2 . , , , , .
, . , , . , , , , ||, ||, .
TL;DR;
, ,
typescript/javascript/angular , , , ||
?.
Angular,
, . , . typescript/javascript/angular
(?.), Angular2
Angular2, javascript.
(? obj: obj)
, , , .
, , ( ), Ternery.
, , , :
: , "", ""
(?:)
javascript, , PHP, , , , , ( ) :
:
let m = something ?: somethingElse // if in case of truthiness of `something` you want to return `something` , you can do this
let m = something ? something : somethingElse
EDIt: , .
javascript(Typescript), HTML,
javascript(Typescript)
HTML
, javascript-, , , ,
angular2 Ternary Operator Safe Navigation Operator (?.) Elvis Operator (?: ), - .
Ternary Operator
Safe Navigation Operator (?.)
Elvis Operator (?: )
alternate: - Elvis Operator (?: ) angular2, ( , )
let gender = user.gender || "";
@Milad RameshRajendran elvis Safe Navigation Operator (?.) angular2, ,
RameshRajendran
- :
" " - Java. , , " " false . : :
def gender = user.male ? "male" : "female" //traditional ternary operator usage def displayName = user.name ?: "Anonymous" //more compact Elvis operator
(?.)NullPointerException. , , , , null . , null , , :
(?.)
NullPointerException. , , , , null . , null , , :
def user = User.find( "admin" ) //this might be null if 'admin' does not exist def streetName = user?.address?.street //streetName will be null if user or user.address is null - no NPE thrown
, . , ?
Source: https://habr.com/ru/post/1017341/More articles:How to enable end-to-end tests through microservices in multiple continuous delivery pipelines? - aseYahoo Finance Historical Data Download Guide Does Not Work - CookiesKapt does not work with AutoValue in Android Studio 3.0 - android-studioVertical alignment elements in a map using Bootstrap 4 - htmlHow do you make sure that a Vimeo video exists? - javascriptHandle invalid msgpack messages sent from IE10 - javascriptУвеличение яркости изображения без переполнения - pythonFabric.io: new app not showing in toolbar - crashlyticsThe route [password.reset] is not defined in laravel 5.4 in ResetPasswords.php - phpAndroid TV: RowsFragment element does not work in several cases - androidAll Articles