constyearsTillDie = (birthYear, myname) => { const age = 2025 - birthYear; const deadYear = 35 - age; return`${myname} will live for only ${deadYear} years from now.`; } const restOfTime = yearsTillDie(2004, "Jerry Yu"); console.log(restOfTime);
//Prevent multiple repeated computation when we have to multiply call the same funtion. clacAge: function () { this.age = 2025 - this.birthYear; console.log(this); returnthis.age; } }; console.log(jerry.clacAge()); // console.log(jerry.clacAge()); // console.log(jerry.clacAge()); console.log(jerry.age); console.log(jerry.age);
while (dice !== 6) { console.log(`You rolled a ${dice}`); dice = Math.trunc(Math.random() * 6) + 1; }
在vscode中control+b可以展开或关闭侧边栏。
DOM
const score0 = document.getElementById("score--0"); const score1 = document.getElementById("score--1"); //manipulate an id element by two different methods. //getElementById() is a litte faster than querySelector(). const score0 = document.querySelector("#score--0");
Deeper understanding to JS
arrow function don’t own arguments object and this keyword in its execution context .But, they can use the arguments object and this key word from their closest regular function parent.
let,const and function are blocked scoped.var is function scoped.
用var创建的变量会成为global window Object的一部分:
var x = 1; let y = 2; const z = 3; console.log(x===window.x); console.log(y===window.y); console.log(z===window.z);
output: true false false
arrow function的thiskey word 是它的 parent function 的thiskey word.
定义object的{}并不创建scope:
const jerry = { firstName: "Jerry", age: 12, greet: function () { console.log(`Hello dear ${this.firstName}`); },
howOld: () =>console.log(`I am ${this.age}`), };
jerry.howOld();
输出I am undefined这里this的parent scope是global scope。this在global中应该为window Object 而 window Object 无age属性。
Data Structures and Modern Operators
Iterables: arrays, strings, maps, sets. NOT objects.
//Functions const add = function (...numbers) { let sum = 0; for (let i = 0; i < numbers.length; i++) sum += numbers[i]; console.log(sum); };
//7. The team with the lower odd is more likely to win. Print to the console which team is more likely to win, WITHOUT using an if/else statement or the ternary operator. team1 < team2 && console.log('Team 1 is more likely to win'); team1 > team2 && console.log('Team 2 is more likely to win');
/* 83. Print the 3 odds to the console, but in a nice formatted way, exaclty like this: Odd of victory Bayern Munich: 1.33 Odd of draw: 3.25 Odd of victory Borrussia Dortmund: 6.5 Get the team names directly from the game object, don't hardcode them (except for "draw"). HINT: Note how the odds and the game objects have the same property names 😉 */ const game = { team1: 'Bayern Munich', team2: 'Borrussia Dortmund', players: [ [ 'Neuer', 'Pavard', 'Martinez', 'Alaba', 'Davies', 'Kimmich', 'Goretzka', 'Coman', 'Muller', 'Gnarby', 'Lewandowski', ], [ 'Burki', 'Schulz', 'Hummels', 'Akanji', 'Hakimi', 'Weigl', 'Witsel', 'Hazard', 'Brandt', 'Sancho', 'Gotze', ], ], score: '4:0', scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'], date: 'Nov 9th, 2037', odds: { team1: 1.33, x: 3.25, team2: 6.5, }, };
for (const [goal, playerName] of game.scored.entries()) { console.log(`Goal ${goal + 1}: ${playerName}`); }
let ave = 0; for (const odd ofObject.values(game.odds)) { ave += odd; } console.log(ave); ///////////////////////////////////////////////////////////////////////////////////////// for (const [team, odd] ofObject.entries(game.odds)) { const teamStr = team === 'x' ? 'draw' : `victory ${game[team]}`; console.log(`Odd of ${teamStr}${odd}`); } ///////////////////////////////////////////////////////////////////////////////////////// /* BONUS: Create an object called 'scorers' which contains the names of the players who scored as properties, and the number of goals as the value. In this game, it will look like this: { Gnarby: 1, Hummels: 1, Lewandowski: 2 } */ const scorers = {}; for (const player of game.scored) { scorers[player] ? scorers[player]++ : (scorers[player] = 1); } console.log(scorers);