View profile

Day #4: Three Ways to CreateArray() as an Object (OOP, part 2)

Puppy Nomadic
Puppy Nomadic
Continuing on from my Day 3 post, where I put my first solution to the CreateArray() problem…
My first way of doing this problem did not hide the array methods in the object. Here are two additional ways to CreateArray(), which hide the various array methods in the prototype of the object:
1) By using Javascript prototypes
2) By using Javascript classes (syntactic sugar equivalent to 1)

1. Using Javascript Prototypes
function createArray() {
this.length = 0;
return this;
}
createArray.prototype.push = function(…val) {
for (let v of val) {
this[this.length.toString()] = v;
this.length++;
}
return this.length;
}
createArray.prototype.pop = function() {
let returnValue = this[this.length - 1];
delete this[(this.length-1).toString()];
this.length–;
return returnValue;
}
createArray.prototype.unshift = function (val) {
for (let i = this.length; i > 0; i–) {
this[i.toString()] = this[(i - 1).toString()];
}
this[(0).toString()] = val;
this.length++;
return this;
}
createArray.prototype.shift = function () {
let returnValue = this[(0).toString()];
for (let i = 0; i < (this.length - 1); i++) {
this[i.toString()] = this[(i + 1).toString()];
}
delete this[(length - 1).toString()];
this.length–;
return returnValue;
}
2. Using Javascript Classes
class Array {
constructor() {
this.length = 0;
}
push(…val) {
for (let v of val) {
this[this.length.toString()] = v;
this.length++;
}
return this.length;
}
pop() {
let returnValue = this[this.length - 1];
delete this[(this.length-1).toString()];
this.length–;
return returnValue;
}
unshift(val) {
for (let i = this.length; i > 0; i–) {
this[i.toString()] = this[(i - 1).toString()];
}
this[(0).toString()] = val;
this.length++;
return this;
}
shift() {
let returnValue = this[(0).toString()];
for (let i = 0; i < (this.length - 1); i++) {
this[i.toString()] = this[(i + 1).toString()];
}
delete this[(length - 1).toString()];
this.length–;
return returnValue;
}
};
function createArray() {
return new Array;
}
Reference
These two snippets from Will Sentance’s Object Oriented JS course on Front End Masters were most helpful for me to arrive at these solutions:
For the JS Prototype version of the createArray() problem
For the JS Prototype version of the createArray() problem
For the JS Class version of the createArray() problem
For the JS Class version of the createArray() problem
Learn the Fundamentals of Object Oriented Programming in JavaScript
For Day 5 of #100DaysOfCode, I’ll include links from yesterday’s study group in which we set up our dev environment using CodeSmith’s recommend VS Code extensions, and jazzed up our Terminal with pokemon and themes.
Did you enjoy this issue? Yes No
Puppy Nomadic
Puppy Nomadic @puppynomadic

Let's pull each other up by our Bootstrap.js 🪴 👩🏻‍💻 🦮

=== Journey of software craftsmanship ===

{"Formerly": ["nonprofit community organizer", "teacher", "researcher", "government director"],
"Future": ["software engineer", "web3 dev", "developer advocate", "technical program manager"]
}

In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Created with Revue by Twitter.