조건부 할당

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:36 판 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)
conditional assignment
조건부 할당

1 JavaScript[ | ]

var b = "world";
var a = a || "hello";
var b = b || "hello";
console.log( a ); // hello
console.log( b ); // world

2 PHP[ | ]

$b = 'world';
$a = isset($a)? $a : 'hello';
$b = isset($b)? $b : 'hello';
var_dump( $a ); // string(5) "hello"
var_dump( $b ); // string(5) "world"
$b = 'world';
isset($a) or $a = 'hello';
isset($b) or $b = 'hello';
var_dump( $a ); // string(5) "hello"
var_dump( $b ); // string(5) "world"
$b = 'world';
$a = @$a ?: 'hello';
$b = @$b ?: 'hello';
var_dump( $a ); // string(5) "hello"
var_dump( $b ); // string(5) "world"

3 Ruby[ | ]

b = 'world'
a ||= 'hello'
b ||= 'hello'
puts a # hello
puts b # world
c = nil
c ||= "hello"
puts c
# hello
d = ""
d ||= "hello"
puts d
#
#arr = [] if arr.nil?
arr ||= []

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}