Understanding Javascript The Weird Part Parts Today

function getObj() return // ASI adds semicolon here → returns undefined ok: true ;

function Dog(name) this.name = name; Dog.prototype.bark = function() return 'woof'; ; const d = new Dog('Rex'); d.bark(); // 'woof' Weird parts: understanding javascript the weird part parts

Here’s a structured guide to understanding the classic (and famously quirky) areas of JavaScript—often referred to as the “weird parts” made popular by Anthony Alicea’s course “JavaScript: Understanding the Weird Parts” . The weird part: You can use variables before they’re declared. function getObj() return // ASI adds semicolon here

const obj = name: 'Alice', greet() console.log(this.name); ; const greetFn = obj.greet; greetFn(); // undefined (default binding, not implicit) Fix: use arrow functions (lexical this ) or .bind() . The weird part: A function “remembers” its lexical scope even when executed outside it. function Dog(name) this.name = name

Scroll to Top