Biết rằng giá trị đối tượng truy xuất bằng Dot notation hoặc Bracket notation. Nhưng cũng có thể truy cập bằng khóa biến (variable key). Xem nhanh.
Dùng Dot và Bracket Notation
Ví dụ
Truy xuất giá trị bằng Dot và Bracket notation. Bracket notation dùng string key.
<html> <body> <script>
let me = { name: 'javascript' };
document.write(me.name);
document.write("</br>");
document.write(me['name']);
</script> </body> </html>
Kết quả
javascript javascript
Dùng Variable Key
Ví dụ
Thay vì Dot/Bracket, dùng variable key truy xuất giá trị.
<html> <body> <script>
let me = { name: 'javascript' };
let key = 'name';
document.write(me[key]);
</script> </body> </html>
Kết quả
javascript