简体   繁体   中英

javascript object literal notation depth

I was scouring the internet but I think I didn't do a proper searching job. My question is regarding to Javascript Object Literal Notation. Example bellow.

var Object = {}; Object.key = value;

When trying to go more than one depth level down ie.
Object.key1.key2 = value;

I'm getting an undefined error. Obviously I'm doing something wrong. Could someone explain this behaviour to me?

Thank you very much!

because Object was initialized via var Object = {}; so you can do Object.key = value; which also initialized Object.key but Object.key1 is not initialized yet.

you need to do Object.key1 = {} before doing Object.key1.key2 = value;

您需要定义该深度的每个对象。

var Object = { key1: { key2: value } };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM