var a = 10
console.log(window)
var b = 20
console.log(a)
var c = a + b
function getChildNodes() {
return this?.childNodes;
}
function append(html) {
const div = document.createElement('div');
div.innerHTML = html;
this?.appendChild?.(div);
return div;
}
getChildNodes(); // undefined
getChildNodes.apply(document.body); // NodeList
append('Hello World!'); // undefined
append.apply(document.body, ['Hello World!']); // HTMLDivElementconst foo = () => {
console.log('foo!');
};
foo();
bar();
baz();
function bar() {
console.log('bar!');
}
function baz() {
var fooVar = 'fooVar';
console.log(fooVar);
console.log(barVar);
console.log(bazVar);
var barVar = 'barVar';
const bazVar = 'bazVar';
}
const getChildNodes = () => {
return this?.childNodes;
};
getChildNodes(); // undefined
getChildNodes.apply(document.body); // undefined
const MyAPI = {
notifyIfRemoved = (element, callback) => {
const observer = new MutationObserver((entries) => {
const [entry] = entries;
if (entry.removedNodes.length) {
this.log('Node Removed: ', entry.removedNodes[0]);
observer.disconnect();
callback.apply(element);
}
});
observer.observe(element);
},
log = (message, ...args) => {
console.log(`MyAPI: ${message}`, ...args);
},
}
const div = document.createElement('div');
div.innerHTML = 'Hello World!';
document.body.appendChild(div);
MyAPI.notifyIfRemoved(div, function() {
console.log('Removed node! ', this.innerHTML);
});
function foo () {
console.log('I am a function declaration!');
}
const foo = function () {
console.log('I am an anonymouse function expression!');
}
const bar = function bar () {
console.log('I am a named function expression!');
}
const MyAPI = {
baz() {
console.log('I am a function expression that is an object member!');
}
}
const baz = MyAPI.baz; // Function expression that no longer knows its scope(() => {
function foo() {
console.log('I only exist inside this block.');
}
var bar = 'I only exist inside this block.';
})();
console.log(`I would throw an error trying to reference ${bar}.`); // ReferenceError: bar is undefined(() => {
var firstExample = 'First Example';
console.log(firstExample); // 'First Example'
var secondExample;
console.log(secondExample); // undefined
secondExample = 'Second Example';
console.log(secondExample); // 'Second Example'
console.log(thirdExample); // undefined
var thirdExample = 'Third Example';
console.log(thirdExample); // 'Third Example'
console.log(fourthExample); // Temporal Dead Zone!
let fourthExample = 'Fourth Example';
console.log(fourthExample); // 'Fourth Example'
})();function Animal(type) {
this.type = type;
this.hunger = 100;
this.isWalking = false;
}
Animal.prototype = {
eat() {
this.hunger = Math.max(this.hunger - 1, 0);
},
stop() {
this.isWalking = false;
},
walk() {
this.isWalking = true;
},
}
function Mammal(type) {
Animal.call(this, type);
}
Mammal.prototype = Object.create(Animal.prototype);
function Dog(name) {
Mammal.call(this, 'dog');
this.name = name;
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.speak = function(message) {
console.log(`${this.name}: Woof!`);
};
function Person(name, age) {
Mammal.call(this, 'human');
this.name = name;
this.age = age;
}
Person.prototype = Object.create(Mammal.prototype);
Person.prototype.speak = function(message) {
console.log(`${this.name}: ${message}`);
};
const me = new Person('Richard', 'too old');
const dog = new Dog('Fido');
me.speak("I'm a real person!"); // "Richard: I'm a real person!"
dog.speak("I'm also a real person!"); // "Fido: Woof!"
console.log(me instanceof Person); // true
console.log(me instanceof Mammal); // true
console.log(me instanceof Animal); // true
console.log(me instanceof Dog); // false
class Animal {
constructor(type) {
this.type = type;
this.hunger = 100;
this.isWalking = false;
}
eat() {
this.hunger = Math.max(this.hunger - 1, 0);
}
stop() {
this.isWalking = false;
}
walk() {
this.isWalking = true;
}
}
class Mammal extends Animal {}
class Dog extends Mammal {
constructor(name) {
super('dog');
this.name = name;
}
speak(message) {
console.log(`${this.name}: Woof!`);
}
}
class Person extends Mammal {
constructor(name, age) {
super('human');
this.name = name;
this.age = age;
}
speak(message) {
console.log(`${this.name}: ${message}`);
}
}
const me = new Person('Richard', 'too old');
const dog = new Dog('Fido');
me.speak("I'm a real person!"); // "Richard: I'm a real person!"
dog.speak("I'm also a real person!"); // "Fido: Woof!"
console.log(me instanceof Person); // true
console.log(me instanceof Mammal); // true
console.log(me instanceof Animal); // true
console.log(me instanceof Dog); // false
fn.