large-coding Flashcards
*How do you declare an object
(literal) const blah = {}, (constructor) new Object(), Object.create()
—————————————————————
Actually there’s 6 ways:
Object Literal Object Constructor Constructor Function Object.create() Method Object.assign() Method ES6 Classes
https://attacomsian.com/blog/javascript-create-object
*How do you declare an array
(literal) const blah = [], (constructor) new Array(), Array.from() ————————————————————— // using the new keywrod const array1 = new Array("1", "2") console.log(array1)
// array literal const array2 = [true, false] console.log(array2)
// using the from method, createds a shallow copy array // doesn't work in IE const array3 = Array.from(array2) console.log(array3)
// you can spread one array into another one const sup = ['1', '2'] const sup2 = [...sup] console.log(sup2)
—————————————————————
Not sure if this counts as declaring an array but … you can create an array from objects like this
// You can create an array from an objects values using Object.values const obj1 = { name: "brad", age: "12", } console.log(Object.values(obj1)) >> [ 'brad', '12' ]
// Or you could do it from an object’s keys using Object.keys
console.log(Object.keys(obj1))
» [‘name’, ‘age’]
*IMplement the revealing module pattern
There’s also something called the ‘revealing module pattern’ where you return stuff out of your IIFE for use elsewhere. For example in the code below I return an object for use:
const myName = (function() { function logFn(){ console.log("hello"); } return {logStuff: logFn} })()
Now I can use stuff like myName.name in other places
How do you implement inheritance
ES6 way with classes: class Player { constructor(name, type) { console.log(this, "From player") //console.log(name, `This is the name inside the Player class`) this.name = name; this.type = type; }
introduce() { console.log(`Hi my name is ${this.name}, I'm a ${this.type}`) } }
class Wizard extends Player { constructor (name, type) { //console.log(name, `This is the name inside of the wizard class`) let othertype = "billy"; let othername = "mark" super(othername, othertype) console.log(this, "From Wizard")
} play() { console.log(`Weeee I'm a ${this.name}, of type ${this.type}`) } }
const wizard1 = new Wizard('Shelly', 'Healer') const wizard2 = new Wizard('Shawn', 'tank')
wizard2. introduce() wizard2. play() wizard1. introduce() wizard1. play() Pre-es6 way with prototypes:
var Player = function(name, type) { this.name = name; this.type = type; }
Player.prototype.introduce = function() { console.log(`Hi I'm ${this.name} of type ${this.type}`) }
var wizard1 = new Player("Shelly", "healer"); var wizard2 = new Player("Billy", "tank");
wizard1.play = function() { console.log(`Weeee, I'm a ${this.type}`) } wizard2.play = function() { console.log(`Woop, I'm also a ${this.type}`) } wizard1. introduce() wizard1. play()
Learn classes and extends from 20:53 https://www.udemy.com/course/advanced-javascript-concepts/learn/lecture/13511600#search
Can relearn classes and prototypes with this udemy section: https://www.udemy.com/course/javascript-beginners-complete-tutorial/learn/lecture/17190250#search
^that’s a setup, with a missing constructor in the child objects, that you would use if you don’t need to add any particular opening variables to the child and just want to use the parents constructor
Video tutorial on this one: https://www.udemy.com/course/javascript-beginners-complete-tutorial/learn/lecture/17190274#announcements
Also figured out how to do it like this:
>>const Player = function(name, age) { this.name = name; this.age = age; }
//Player.prototype = new Player("billy", 12) Player.prototype.sayname = function() { console.log(`My name is ${this.name} and I am ${this.age} old`) }
>>const Athlete = function(sport, name, age) { Player.call(name, age) this.sport = sport; }
Athlete.prototype.saysport = function() {
console.log(I play ${this.sport}
)
}
const bobby = new Athlete(“soccer”, “billy”, 12)
bobby.saysport()
bobby.__proto__ = new Player(“billy”, 12)
//console.log(bobby.__proto__)
bobby.sayname()
*How many types are there in javascript and what are they
There are 7:
booleans strings numbers symbol undefined null
object - this is a non primitive type as it doesn't have the value directly like the others. arrays, and function are objects too
*Quickly convert a variable to a number
you could use unary plus
console.log(+”10”)
console.log(Number(“10”)) is also correct
*loop through the properties of an object
const obj = { name: "billy", age: 12, value: false, }
const obj2 = { color: "blue" } obj2.\_\_proto\_\_ = obj
console.log(obj2.color)
for (const prop in obj2) { if (obj2.hasOwnProperty(prop)){ // use hasOwnProperty so you don't get the prototypes properties as console.log(prop, obj[prop]) } }
for (const [key, value] of Object.entries(obj2)){ console.log(key, value) } for (const key of Object.keys(obj2)){ console.log(key) } for (const value of Object.values(obj2)){ console.log(value) }
*quickly convert a value to boolean
console.log(!!variable)
*use a property accessor with bracket notation to assign something to an object property
const fun = { thing: { yes: true, no: false }["no"] }
console.log(fun.thing)
*What does && and || do? And which is executed first?
returns the value of the first falsy, so i presume || returns the value of the first truthy?
console.log(“abc” && 10 && NaN && “lol”)
Logical and (&&) is executed first:
console. log(“true” && false && true || console.log(“sup”)) // sup \n undefine
https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND
*HOw would you access the first element of an array?
Index 0, bracket notation, Array.at()
—————————————————————
const coolarray = [‘1’, ‘2’, ‘3’]
// bracket notation console.log(coolarray[0])
// using the at method // according to MDN this is experimental and may change in the future // doesn't work in IE console.log(coolarray.at(1))
// using the shift method // this mutates the array console.log(coolarray.shift())
// destructuring const anotherarray = ['1','2','3'] const [billy] = anotherarray console.log(billy)
const sample = [[‘1’, ‘2,’, ‘3’], [‘4’, ‘5,’, ‘6’]]
let [[a], [b]] = sample;
console.log(a, b); // “1” “4”
—————————————————————
This covers destructuring array
https://www.youtube.com/watch?v=giNjEgYTd9E
*How do you create an instance of an object?
new keyword using class constructor, Object.create()
—————————————————————
You can use the new keyword to initialize an instance of Object:
const fruits = new Object();
You can use the class keyword to define a new class in JavaScript instead of a function constructor, and then use the new keyword to create an instance of it.
class User { constructor(name, age) { this.name = name; this.age = age; }
sayHi() { return `Hi ${this.name} 👋`; } }
const user = new User(‘Atta’, 30);
console. log(user.sayHi()); // Hi Atta 👋
console. log(user.age); // 30
–
The Object.create() method allows us to create a new object, using an existing object as the prototype of the newly created object. This method is especially useful when you want to create a new object from an already existing object.
const Vehicle = { maker: 'BMW', color: 'Black' };
const vehicle = Object.create(Vehicle);
console. log(vehicle.maker); // Tesla
console. log(vehicle.color); // Red
*Build an old-school for loop and explain it
The for loop has the following syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block. Statement 3 is executed (every time) after the code block has been executed.
*https://leetcode.com/problems/contains-duplicate/
a
*https://leetcode.com/problems/valid-anagram/
a