mirror of
https://github.com/hi-language/hi-language.github.io.git
synced 2026-01-14 00:28:05 +00:00
Refactor: Clarify implicit return behavior and simplify example
This commit is contained in:
15
index.html
15
index.html
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
<!-- Google Fonts -->
|
<!-- Google Fonts -->
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
<link rel="preconnect" href="https://fonts.gstatic" crossorigin>
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&family=JetBrains+Mono:wght@400;700&display=swap" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&family=JetBrains+Mono:wght@400;700&display=swap" rel="stylesheet">
|
||||||
|
|
||||||
<!-- Markdown & Highlighting Styles -->
|
<!-- Markdown & Highlighting Styles -->
|
||||||
@@ -137,12 +137,15 @@ counter: {
|
|||||||
|
|
||||||
inc: {
|
inc: {
|
||||||
value = value + 1 // Mutates parent's private state
|
value = value + 1 // Mutates parent's private state
|
||||||
_("The count is now: " + value)
|
}
|
||||||
|
|
||||||
|
get: {
|
||||||
|
value // The last expression is implicitly returned
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
counter.inc() // Prints "The count is now: 1"
|
counter.inc()
|
||||||
counter.inc() // Prints "The count is now: 2"
|
_(counter.get()) // Prints 1
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
### Blocks with Parameters
|
### Blocks with Parameters
|
||||||
@@ -238,13 +241,15 @@ sum(5, mul(2, 3)) // 11
|
|||||||
|
|
||||||
## Method Chaining
|
## Method Chaining
|
||||||
|
|
||||||
By default, a method call on a Block returns the Block itself (\`this\`), enabling fluent, chainable interfaces. To return a different value and break the chain, use the return symbol \`^\`.
|
A Block's return behavior is designed for fluency. It implicitly returns the value of its last expression. If the last expression does not yield a value (such as an assignment), the Block returns itself (\`this\`) by default, enabling method chaining. The \`^\` symbol is used for an explicit or early return from any point in the Block.
|
||||||
|
|
||||||
\`\`\`javascript
|
\`\`\`javascript
|
||||||
calculator: {
|
calculator: {
|
||||||
#total: 0
|
#total: 0
|
||||||
|
// Last expression is an assignment, so 'this' is returned
|
||||||
add: (n) { total = total + n }
|
add: (n) { total = total + n }
|
||||||
sub: (n) { total = total - n }
|
sub: (n) { total = total - n }
|
||||||
|
// Last expression is a value, which is returned
|
||||||
get: { ^ total }
|
get: { ^ total }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user