Using dot (.) vs using ([]) to access properties: →
course.section;
course["section"];
when using dot, the part after the dot directly names the property
the property name must be a valid variable names. (what were they again? →)
start with a letter, underscore ( _ ), or dollar ( $ )
following characters can be any of above, and/or digits (0-9)
when using square brackets, the part within the brackets is evaluated and is used as the property name
this allows for dynamically created property names
also allows property names that are not valid variable names obj["I'm ok"] = true (oof, maybe avoid that))
Dynamic Properties
The code below uses brackets for dynamic property access →
it asks for user input, specifying a key / property name
and it should output the value at the key
// using the same object from previous slides...const course = { name:'Applied Internet Technology', section:8, undergraduate:true };
// setting up user inputconst readline = require('readline');
const p = readline.createInterface({ input: process.stdin, output: process.stdout });
p.question('Type in an object key\n>', function (resp) {
// TODO: print value at key
p.close();
});
Here, we have to use bracket notation: console.log(course[resp]).
Property Names
All property names are turned into a string prior to being used as a key
This poses a bit of a problem:
any object can be used as a key
but the string version of two different objects may be the same!
by default objects that we create are converted to the string [object Object] when cast to a String.
for example: String({}) returns [object Object]
Objects as Property Names!? Don't
Here's an example of the issue:
const k1 = {foo:'bar'};
const k2 = {baz:'qux'};
const obj = {};
obj[k1] = 1;
obj[k2] = 2;
console.log(obj);
// { '[object Object]': 2 }// uh oh! ... you can see that there's only one prop
Methods (Again)
It's worthwhile to repeat that an object property can be a function.
if object's property is a function, it's sometimes called a method
It's pretty common to create methods on objects, so ES6 introduces a shortcut for creating methods on objects simply by setting properties equal to function expressions: →
Note that you can chain method calls or property access on objects (if the prior method call or property returns an object with the next method/attribute accessible__→
We actually get a runtime error because null does not have a method called pop.
(we would have to check each intermediary object for a null or undefined value to prevent this kind of error)
Optional Chaining
The optional chaining operator, ?., helps prevent runtime errors, and instead results in undefinedbeing returned if a property access or method call in the chain results in null or undefined →
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2022-01-26 Day 13 Sort Algorithms Part I (Bubble Sort & Merge Sort)
2021-01-26 Discussion 2
2021-01-26 一种数据结构:SLList