Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch3 "Objects-Classes" getX() should return 3, not 21 #1857

Closed
wants to merge 1 commit into from

Conversation

TylerWoolcott
Copy link

@TylerWoolcott TylerWoolcott commented Jan 12, 2024

Please type "I already searched for this issue":
I already searched for this issue
Edition: (pull requests not accepted for previous editions)
2nd
Book Title:
objects-classes
Chapter:
3
Section Title:
Overiding methods
Topic:
For the code example (below), shouldn't the result of point.printX(); be x: 3 instead? Given that Point3d is a subclass of Point2d, when Point3d is instantiated as point using the super keyword, it inherits the properties of Point2d:
a. super.getX() refers to the getX() method in Point2d.
b. The getX() method in Point2d returns the x property of Point2d, which is 3.
c. Hence, printX() outputs x: 3.

The code example from the chapter:
class Point2d {
x = 3
y = 4

getX() {
    return this.x;
}

}

class Point3d extends Point2d {
x = 21
y = 10
z = 5

getX() {
    return this.x * 2;
}
printX() {
    console.log(`x: ${super.getX()}`);
}

}

var point = new Point3d();

point.printX(); // x: 21

@getify
Copy link
Owner

getify commented Jan 12, 2024

I copied and pasted the code you quoted above (same as in the book chapter). Here's the output:

console-output

That's consistent with what I indicated in the chapter.

The reasoning you asserted is inaccurate: "The getX() method in Point2d returns the x property of Point2d, which is 3."

The getX() method in Point2d returns the this.x instance property, not a static property of Point2d (nor a lexical x variable). The this reference here resolves to the point object, since that's the call-site. And that object has the x value of 21 from the Point3d() constructor.


Side note: you've filed this as a PR against the issue template, as opposed to just as a normal issue. That's a confusing way to do it, so I'm going to close this. If you feel you want to discuss further, please open a new regular issue.

@getify getify closed this Jan 12, 2024
Repository owner locked as resolved and limited conversation to collaborators Jan 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants