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

Add example with constructor props #1056

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion book/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,23 @@ returning the value (which will always be `nil`), we again return `this`.

Phew! That was a whole list of tasks but our reward is that our little
interpreter has grown an entire programming paradigm. Classes, methods, fields,
`this`, and constructors. Our baby language is looking awfully grown-up.
`this`, and constructors. Our baby language is looking awfully grown-up. If you
want to give it a try, here's a script to run:

```lox
class Cake {
init(adjective) {
this.adjective = adjective;
}
taste() {
print "The " + this.flavor + " cake is " + this.adjective + "!";
}
}

var cake = Cake("scrumptious");
cake.flavor = "German chocolate";
cake.taste(); // Prints "The German chocolate cake is scrumptious!".
```

<div class="challenges">

Expand Down