// this get method uses the lengths of this instance
let showLengths = new MoreLengths(2,2,2,5)
console.log(showLengths.getLengths())
console.log(MoreLengths.getLengths())
Assistant
Let's break down why you're seeing errors and undefined results. The core issues lie in how you're initializing properties in the Lengths class constructor and how you're attempting to use static methods.
The Problem: In JavaScript constructors, this.propertyName only gets assigned a value after you explicitly do this.propertyName = someValue;.
When your Lengths constructor runs this.items = [this.firstLength, this.secondLength, this.thirdLength], the properties this.firstLength, this.secondLength, and this.thirdLength have not yet been assigned any values. They are undefined at this point.
So, this.items is initialized as [undefined, undefined, undefined].
Crucially, the arguments firstLength, secondLength, thirdLength passed to the constructor are never assigned to the instance's properties.
It assigns values fromthis.items back to this.firstLength, this.secondLength, etc. Since this.items is [undefined, undefined, undefined], this effectively sets this.firstLength, this.secondLength, and this.thirdLength all to undefined.
It completely ignores the arguments set1, set2, set3 that are passed to it. The intention was likely to update the lengths, but it's doing the opposite and not using the new values.
instance.getLengths()
Result: Because this.items was initialized to [undefined, undefined, undefined] and setLengths doesn't use the constructor arguments or the arguments correctly, returns .
setLengths
instance.getLengths()
[undefined, undefined, undefined]
static getLengths()
The Error:TypeError: Cannot read properties of undefined (reading '0')
The Problem: Static methods belong to the class itself, not to an instance. Inside a static method, this refers to the Lengths class, not instance.
The Lengths class does not have a property named items. this.items is an instance property.
When Lengths.getLengths() is called, this is Lengths. this.items evaluates to Lengths.items, which is undefined.
Trying to access undefined[0] (which is undefined[0]) throws the TypeError.
Execution Trace of Lengths
javascript
let instance = new Lengths(2,4,8)
// Constructor runs:
// this.firstLength is undefined
// this.secondLength is undefined
// this.thirdLength is undefined
// this.items = [undefined, undefined, undefined] // Arguments 2,4,8 are lost
instance.setLengths(3,5,9)
// this.firstLength = this.items[0] (which is undefined) -> undefined
// this.secondLength = this.items[1] (which is undefined) -> undefined
// this.thirdLength = this.items[2] (which is undefined) -> undefined
// Arguments 3,5,9 are ignored
console.log(instance.getLengths()) // Outputs: [undefined, undefined, undefined]
// This calls instance.getLengths() which returns [this.items[0], this.items[1], this.items[2]] -> [undefined, undefined, undefined]
// console.log(Lengths.getLengths()) // If uncommented:
// This would call the static method. 'this' inside it is the Lengths class.
// Lengths.items is undefined.
// Accessing undefined[0] throws TypeError.
Problems in the MoreLengths Class
Constructor (super(...) and this.lengthsArray)
The Problem with super: When new MoreLengths(2, 2, 2, 5) is called, super(2, 2, 2) executes the Lengths constructor. As we saw, this sets this.items to [undefined, undefined, undefined].
The Good Part:this.fourthLength = fourthLength; correctly sets this.fourthLength.
The Excellent Part:this.lengthsArray = [firstLength, secondLength, thirdLength, fourthLength]correctly creates an array using the arguments passed to the MoreLengths constructor. This array holds the actual desired values.
getLengths() Instance Method
Result:console.log(showLengths.getLengths()) outputs [2, 2, 2, 5]. This works because this method correctly uses this.lengthsArray, which was populated correctly in the MoreLengths constructor, and it doesn't rely on the broken this.items from the parent class.
static getLengths() (Commented Out)
The Problem: If you were to uncomment this, it would have the exact same issue as the static method in Lengths. this.lengthsArray is an instance property. this inside the static method refers to the MoreLengths class, which does not have a lengthsArray property. This would lead to a TypeError: Cannot read properties of undefined (reading '0').
console.log(MoreLengths.getLengths())
The Error:TypeError: MoreLengths.getLengths is not a function
The Problem: This line explicitly calls the getLengths method that is marked as static. However, the static getLengths() method in your MoreLengths class is commented out. Therefore, when you try to call it as a static method, JavaScript reports that no such function exists on the class.
How to Fix the Lengths Class (if it were intended to store lengths)
You need to assign the constructor arguments to the instance properties, and setLengths should use its arguments.
javascript
class Lengths {
constructor(firstLength, secondLength, thirdLength) {
// Assign constructor arguments to instance properties FIRST
this.firstLength = firstLength;
this.secondLength = secondLength;
this.thirdLength = thirdLength;
// Then, if you want an array of them:
this.items = [this.firstLength, this.secondLength, this.thirdLength];
}
// This method should use its arguments to UPDATE the instance properties
setLengths(set1, set2, set3) {
this.firstLength = set1;
this.secondLength = set2;
this.thirdLength = set3;
// Optionally update the items array if you want it to reflect the latest
this.items = [this.firstLength, this.secondLength, this.thirdLength];
}
// This instance method returns the current instance properties
getLengths() {
// It's usually better to return the actual properties, not the internal array if possible
// Or, if items is the single source of truth, return that.
// Given the constructor setup, returning items is consistent with the original (flawed) intent.
return [this.firstLength, this.secondLength, this.thirdLength];
// Or return this.items;
}
// If you want a static method, it needs access to class-level data,
// or it needs to accept data to operate on.
// For example, a static method to calculate something from given values:
static calculateTotal(l1, l2, l3) {
if (typeof l1 !== 'number' || typeof l2 !== 'number' || typeof l3 !== 'number') {
return undefined; // Or throw an error
}
return l1 + l2 + l3;
}
}
// --- Testing the corrected Lengths class ---
console.log("--- Corrected Lengths ---");
let instance = new Lengths(2, 4, 8);
console.log("Initial instance.getLengths():", instance.getLengths()); // Outputs: [2, 4, 8]
instance.setLengths(3, 5, 9);
console.log("After instance.setLengths(3,5,9):", instance.getLengths()); // Outputs: [3, 5, 9]
// To call the static method correctly:
console.log("Static calculation:", Lengths.calculateTotal(1, 2, 3)); // Outputs: 6
console.log("Static calculation with instance values:", Lengths.calculateTotal(instance.firstLength, instance.secondLength, instance.thirdLength)); // Outputs: 17
Because the Lengths constructor didn't assign constructor arguments to instance properties (this.firstLength = firstLength;), this.items was initialized with undefined values.
The setLengths method was also flawed, not using its arguments and copying undefined values.
console.log(MoreLengths.getLengths()) throws TypeError: MoreLengths.getLengths is not a function:
Because the static getLengths() method in MoreLengths was commented out. If it were uncommented, it would throw a TypeError: Cannot read properties of undefined (reading '0') because this.lengthsArray is an instance property, not a static class property.
The MoreLengths class's instance method getLengths() works because it correctly uses this.lengthsArray which was populated with the constructor's arguments.