YDKJS
Parminder Singh
Senior Frontend Engineer @ Finder UK
Color of marble is "usually determined" - compile step
<script type="text/javascript" src="ngx-slick-carousel.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
What happens when marble is uncolored?
var studentName = "Suzy";
function printStudent(studentName) {
studentName = studentName.toUpperCase();
console.log(studentName);
}
printStudent("Frank");
// FRANK
printStudent(studentName);
// SUZY
console.log(studentName);
// Suzy
var studentName = "Suzy";
function printStudent(studentName) {
console.log(studentName);
console.log(window.studentName);
}
printStudent("Frank");
// "Frank"
// "Suzy"
Global unshadowing trick - anti-pattern
var studentName = "Suzy";
function printStudent(studentName) {
console.log(studentName);
console.log(window.studentName);
}
printStudent("Frank");
// "Frank"
// "Suzy"
Global unshadowing trick - anti-pattern
var one = 1;
let notOne = 2;
const notTwo = 3;
class notThree {}
console.log(window.one); // 1
console.log(window.notOne); // undefined
console.log(window.notTwo); // undefined
console.log(window.notThree); // undefined
var special = 42;
function lookingFor(special) {
// The identifier `special` (parameter) in this
// scope is shadowed inside keepLooking(), and
// is thus inaccessible from that scope.
function keepLooking() {
var special = 3.141592;
console.log(special);
console.log(window.special);
}
keepLooking();
}
lookingFor(112358132134);
// 3.141592
// 42
var special = 42;
function lookingFor(special) {
var another = {
special: special
};
function keepLooking() {
var special = 3.141592;
console.log(special);
console.log(another.special); // Ooo, tricky!
console.log(window.special);
}
keepLooking();
}
lookingFor(112358132134);
// 3.141592
// 112358132134
// 42
Copying Is Not Accessing
what if I'd used objects or arrays as the values instead of the numbers (112358132134, etc.)? - still can't reassign special parameter
function something() {
var special = "JavaScript";
{
let special = 42; // totally fine shadowing
// ..
}
}
Illegal Shadowing
function another() {
// ..
{
let special = "JavaScript";
{
var special = "JavaScript";
// ^^^ Syntax Error
// ..
}
}
}
Illegal Shadowing
let prohibits var to cross the boundaries (or hop over)
function another() {
// ..
{
let special = "JavaScript";
ajax("https://some.url",function callback(){
// totally fine shadowing
var special = "JavaScript";
// ..
});
}
}
Illegal Shadowing
Boundary-crossing prohibition effectively stops at each function boundary
Illegal Shadowing
function something() {
var special = "JavaScript";
{
let special = 42; // totally fine shadowing
// ..
}
}
function another() {
// ..
{
let special = "JavaScript";
ajax("https://some.url",function callback(){
// totally fine shadowing
var special = "JavaScript";
// ..
});
}
}
function askQuestion() {
// ..
}
Function Declaration
Function Expression
var askQuestion = function(){
// ..
};
What happens to the name identifier of the function?
var askQuestion = function ofTheTeacher(){
// ..
};
What happens to the name identifier of the function?
var askQuestion = function ofTheTeacher() {
console.log(ofTheTeacher);
};
askQuestion();
// function ofTheTeacher()...
console.log(ofTheTeacher);
// ReferenceError: ofTheTeacher is not defined
What happens to the name identifier of the function?
var askQuestion = function ofTheTeacher() {
console.log(ofTheTeacher);
};
askQuestion();
// function ofTheTeacher()...
console.log(ofTheTeacher);
// ReferenceError: ofTheTeacher is not defined
It's also read-only
var askQuestion = function ofTheTeacher() {
"use strict";
ofTheTeacher = 42; // TypeError
//..
};
askQuestion();
// TypeError
Function expression with no name identifier?
var askQuestion = function(){
// ..
};
Named function expression
Without name - anonymous function expression.
Shorter way to write functions
var askQuestion = () => {
// ..
};
var askQuestion = () => {
// ..
};
askQuestion.name; // askQuestion
Syntactic brevity
() => 42;
id => id.toUpperCase();
(id,name) => ({ id, name });
(...args) => {
return args[args.length - 1];
};
Claim: Arrow functions somehow behave differently with respect to lexical scope from standard function functions.
Claim: Arrow functions somehow behave differently with respect to lexical scope from standard function functions.
Incorrect!