File tree Expand file tree Collapse file tree
1-js/04-object-basics/01-object Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,13 +2,12 @@ importance: 5
22
33---
44
5- # Hello, object
5+ # 你好,对象
66
7- Write the code, one line for each action:
8-
9- 1 . Create an empty object ` user ` .
10- 2 . Add the property ` name ` with the value ` John ` .
11- 3 . Add the property ` surname ` with the value ` Smith ` .
12- 4 . Change the value of the ` name ` to ` Pete ` .
13- 5 . Remove the property ` name ` from the object.
7+ 按下面的要求写代码:
148
9+ 1 . 创建一个空的 ` user ` 对象.
10+ 2 . 为这个对象增加一个属性,键是 ` name ` 值是 ` John ` 。
11+ 3 . 再增加一个属性键` surname ` 值 ` Smith ` 。
12+ 4 . 把 ` name ` 属性的值改成 ` Pete ` 。
13+ 5 . 从对象中删除 ` name ` 属性。
Original file line number Diff line number Diff line change 11function isEmpty ( obj ) {
22 for ( let key in obj ) {
3- // if the loop has started, there is a prorty
3+ // 如果进到循环里面,说明有属性。
44 return false ;
55 }
66 return true ;
Original file line number Diff line number Diff line change 1- Just loop over the object and ` return false ` immediately if there's at least one property.
1+ 遍历一个对象,如果对象存在任何属性则 ` return false ` 。
22
33``` js
44function isEmpty (obj ) {
Original file line number Diff line number Diff line change @@ -2,11 +2,11 @@ importance: 5
22
33---
44
5- # Check for emptiness
5+ # 检查空对象
66
7- Write the function ` isEmpty(obj) ` which returns ` true ` if the object has no properties, ` false ` otherwise.
7+ 写一个方法 ` isEmpty(obj) ` ,当对象没有属性的时候返回 ` true ` ,否则返回 ` false ` 。
88
9- Should work like that:
9+ 应该像这样:
1010
1111``` js
1212let schedule = {};
Original file line number Diff line number Diff line change 1- Sure, it works, no problem.
1+ 当然,这没什么问题。
22
3- The ` const ` only protects the variable itself from changing.
3+ 关键字 ` const ` 保证存储对象的变量不改变。
44
5- In other words, ` user ` stores a reference to the object. And it can't be changed. But the content of the object can.
5+ 换句话说, ` user ` 保存着一个对对象的引用,引用不能被改变。但是对象可以。
66
77``` js run
88const user = {
99 name: " John"
1010};
1111
1212* ! *
13- // works
13+ // 成功
1414user .name = " Pete" ;
1515*/ ! *
1616
17- // error
17+ // 错误
1818user = 123 ;
1919```
Original file line number Diff line number Diff line change @@ -2,17 +2,17 @@ importance: 5
22
33---
44
5- # Constant objects?
5+ # 不可变对象
66
7- Is it possible to change an object declared with ` const ` , how do you think?
7+ 有可能改变 ` const ` 修饰的对象吗, 你怎么看呢?
88
99``` js
1010const user = {
1111 name: " John"
1212};
1313
1414* ! *
15- // does it work?
15+ // 这样可以吗?
1616user .name = " Pete" ;
1717*/ ! *
1818```
Original file line number Diff line number Diff line change @@ -2,9 +2,9 @@ importance: 5
22
33---
44
5- # Sum object properties
5+ # 属性值求和
66
7- We have an object storing salaries of our team:
7+ 我们用一个对象保存我们团队的工资:
88
99``` js
1010let salaries = {
@@ -14,6 +14,6 @@ let salaries = {
1414}
1515```
1616
17- Write the code to sum all salaries and store in the variable ` sum ` . Should be ` 390 ` in the example above.
17+ 写一段代码求出我们的工资总和,保存到 ` sum ` ,从上面看很明显应该是 ` 390 ` 。
1818
19- If ` salaries ` is empty, then the result must be ` 0 ` .
19+ 如果 ` salaries ` 是一个空对象,那结果应该是 ` 0 ` 。
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ let menu = {
77
88function multiplyNumeric ( obj ) {
99
10- /* your code */
10+ /* 你的代码 */
1111
1212}
1313
Original file line number Diff line number Diff line change @@ -2,14 +2,14 @@ importance: 3
22
33---
44
5- # Multiply numeric properties by 2
5+ # 数值属性都乘以 2
66
7- Create a function ` multiplyNumeric(obj) ` that multiplies all numeric properties of ` obj ` by ` 2 ` .
7+ 创建一个 ` multiplyNumeric(obj) ` 方法,把 ` obj ` 所有的数值属性都乘以 ` 2 ` 。
88
9- For instance:
9+ 例如:
1010
1111``` js
12- // before the call
12+ // 在调用之前
1313let menu = {
1414 width: 200 ,
1515 height: 300 ,
@@ -18,16 +18,16 @@ let menu = {
1818
1919multiplyNumeric (menu);
2020
21- // after the call
21+ // 调用方法之后
2222menu = {
2323 width: 400 ,
2424 height: 600 ,
2525 title: " My menu"
2626};
2727```
2828
29- Please note that ` multiplyNumeric ` does not need to return anything. It should modify the object in-place.
29+ 注意 ` multiplyNumeric ` 方法不返回任何值,它改变了传入的对象。
3030
31- P.S. Use ` typeof ` to check for a number here.
31+ P.S. 用 ` typeof ` 检查值类型。
3232
3333
You can’t perform that action at this time.
0 commit comments