조건부 할당

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 }}