Regular Functions
function someMethod(){
console.log('Inside someMethod')
return true
}
ES6 version
const someMethod = () => {
console.log('Inside someMethod')
return true
}
Can also be used as anonymous function
Anonymous Arrow Function
document.addEventListener('click',(e)=>{
//Handle Click
})
Implicit Return
const doubleIt = x => x * 2 ;
Implicit Return Object
const returnObject = () => ({name:'John',age:30}) ;
- Does not have its own bindings to
this
orsuper
, and should not be used as methods. - Does not have
new.target
keyword. - Not suitable for
call
,apply
andbind
methods, which generally rely on establishing a scope. - Can not be used as constructors.
whatami = this
var whatami = this //function scoped
window.whatami = this
//===================
let blockScoped = 'Some Other value' //let is bock scoped, can change or be mutated
const man = {name:'Jhon'} //const is block scoped, can be mutated but can't change
console.log(this) //global or window object
//Ojbects================
{
const person = {name:'amir'}
const person2 = {name:'amir'}
const person3 = person
console.log(person === person2) // false
console.log(person === person3) // true
}
console.log(person) //undefined
//Arrays=================
const arr = []
const arr2 = []
arr = array.map
console.log(arr === arr2) //false
...
is Spread
const fruits = ['apple','orange','banana']
console.log(...fruits) // 'apple orange banana'
console.log('apple','orange','banana') // 'apple orange banana'
const messages = {
sour : "Oh, this is sour",
sweet: "Waow, this is so sweet"
}
function someFunction(fruit1,fruit2,fruit3){
console.log(fruit1,":",this.sweet)
console.log(fruit2,":",this.sour)
console.log(fruit3,":",this.sweet)
}
someFunction.call(messages,...fruits)
const newTaste = "New Taste"
const moreMessages = {
...messages,
bitter: "Eewoooo, Yukh",
tasteless: "Whatever",
newtaste,
}
...Rest
function printMyName(...value) {
return value;
}
printMyName("Oluwatobi", "Sofela") //["Oluwatobi", "Sofela"]
const [firstName, lastName, ...otherInfo] = [
"amir", "hameed", "coder", "Web Developer", "Male"
];
console.log(otherInfo); //["coder", "Web Developer", "Male"]
export function Input(props) {
const { type = 'input', classes = '', value = '', placeholder = '', events, attributes } = props
return type === 'input' ? (
<input
class={defaultClasses.input + ' ' + classes}
value={value}
placeholder={placeholder}
{...attributes}
{...events}
/>
) : (
<textarea
class={`${defaultClasses.input} ${classes}`}
value={value}
placeholder={placeholder}
{...attributes}
{...events}
/>
)
}
const fruits = ['apple','orange','banana']
const [apple,orange] = fruits
const messages = {
sour : "Oh, this is sour",
sweet: "Waow, this is so sweet"
}
}
const {sweet,sour, newTaste='something new'} = messages;
console.log(newTaste)