Skip to content
Snippets Groups Projects
Commit 589f5636 authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

updating code examples

parent f13bb7b2
No related branches found
No related tags found
No related merge requests found
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
.completed {text-decoration: line-through;}
</style>
<title>Event Bubbling</title>
</head>
<body>
<ol class="list">
<li class="item">Complete Exercise 6</li>
<li class="item">Complete Project 4</li>
<li class="item">Complete Final Part 1</li>
<li class="item">Complete Final Part 2</li>
</ol>
<script>
const list = document.querySelector(".list");
list.addEventListener("click", e => {
e.target.classList.toggle("completed")
console.log(e.currentTarget)
})
</script>
</body>
</html>
\ No newline at end of file
// What does new do under the hood
// Let's create our own new
// function customNew(constructor) {}
// 1. create an empty object
// var obj = {}
// 2. set the prototype
// Object.setPrototypeOf(obj, constructor.prototype)
// 3. execute constructor with this
// ES6 syntax
// var argsArray = Array.from(arguments).slice(1)
//constructor.apply(obj, argsArray)
// Vanilla JS/ES5 syntax
// var argsArray = Array.prototype.slice.call(arguments)
// constructor.apply(obj, argsArray.slice(1))
// 4. Return the created object
// Returns this if the function doesn't return an object
// first attempt - return obj
// edge case correction
// return object from function or this
function Car(make, model, year) {
this.make = make
this.model = model
this.year = year
//return {
//mindBlown: true
//}
}
Car.prototype.getMake = function() {
console.log(`I am a ${this.make}`)
}
function customNew(constructor) {
var obj = {}
Object.setPrototypeOf(obj, constructor.prototype)
console.log(arguments)
//var argsArray = Array.from(arguments).slice(1)
var argsArray = Array.prototype.slice.call(arguments)
//constructor.apply(obj, argsArray.slice(1))
//return obj
return constructor.apply(obj, argsArray.slice(1)) || obj
}
var tesla = customNew(Car, 'tesla', 'ugly truck from superbowl', 2099)
//var foo = ['one', 'two', 'three']
//console.log(Array.prototype.slice.call(foo))
//console.log(foo)
console.log(tesla)
tesla.getMake()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment