React: Difference between revisions
javascript |
Returning an object |
||
Line 64: | Line 64: | ||
const lordify = firstName => `${firstName} of Canterbury`; | const lordify = firstName => `${firstName} of Canterbury`; | ||
</source> | |||
Returning an object -- '''DON'T FORGET PARENTHESES!''' | |||
<source lang="js"> | |||
const person = (firstName, lastName) => ({ | |||
first: firstName, | |||
last: lastName | |||
}); | |||
console.log(person("Flad", "Hanson")); | |||
</source> | </source> |
Revision as of 19:26, 30 September 2019
Installation
// initialize a nodejs project (creates package.json)
$ npm init -y
// package manager
$ npm install yarn
$ yarn install packagename
$ yarn remove packagename
JavaScript
Three ways to declare variables are
- const
- var
- let
Template string
console.log(`${lastName}, ${firstName} ${middleName}`);
document.body.innerHTML = `
<section>
<header>
<h1>The React Blog</h1>
</header>
<article>
<h2>${article.title}</h2>
${article.body}
</article>
<footer>
<p>copyright ${new Date().getYear()} | The React Blog</p>
</footer>
</section>
`;
Function declaration vs function expression: declarations are hoisted
const f = function() {
};
Arrow functions
const lordify = function(firstName) {
return `${firstName} of Canterbury`;
};
// equals
const lordify = firstName => `${firstName} of Canterbury`;
Returning an object -- DON'T FORGET PARENTHESES!
const person = (firstName, lastName) => ({
first: firstName,
last: lastName
});
console.log(person("Flad", "Hanson"));