b && b.c && b.c.d是否有快捷方式? b.c.d:“默认”;

时间:2019-04-29 20:27:43

标签: javascript ecmascript-6

如果我想使用嵌套对象属性中的值设置一个常量,该值可以是未定义的或将其设置为默认值,我可以这样做:

const a = b && b.c && b.c.d ? b.c.d : "default value";

我不喜欢这种语法,因为我发现它不可读。我更喜欢:

const a = b.c.d || "default value";

但是,如果bc不是对象(未定义),则此语法失败。有这种需要的语法吗?

1 个答案:

答案 0 :(得分:0)

我认为这会起作用,

Hibernate: select country0_.country_id as country_1_12_0_, country0_.name as name2_12_0_, country0_.phonecode as phonecod3_12_0_ from country country0_ where country0_.country_id=?
country: India
Hibernate: select states0_.country_id as country_3_38_0_, states0_.state_id as state_id1_38_0_, states0_.state_id as state_id1_38_1_, states0_.name as name2_38_1_ from state states0_ where states0_.country_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
Hibernate: select cities0_.state_id as state_id3_10_0_, cities0_.city_id as city_id1_10_0_, cities0_.city_id as city_id1_10_1_, cities0_.name as name2_10_1_ from city cities0_ where cities0_.state_id=?
state: Punjab
state: Lakshadweep
state: Kerala
state: Maharashtra
state: Himachal Pradesh
state: Madhya Pradesh
state: Natwar
state: Tamil Nadu
state: Gujarat
state: Uttarakhand
state: Delhi
state: Pondicherry
state: Haryana
state: Jammu and Kashmir
state: Rajasthan
state: Chandigarh
state: Sikkim
state: Arunachal Pradesh
state: Vaishali
state: Kenmore
state: Andhra Pradesh
state: Daman and Diu
state: Paschim Medinipur
state: Tripura
state: Chhattisgarh
state: Uttar Pradesh
state: Jharkhand
state: Manipur
state: Meghalaya
state: Andaman and Nicobar Islands
state: Mizoram
state: Goa
state: Karnataka
state: Dadra and Nagar Haveli
state: Odisha
state: West Bengal
state: Bihar
state: Assam
state: Narora
state: Nagaland
state: Telangana