Support for anonymous functions.
This commit is contained in:
@@ -17,23 +17,22 @@ Rhai's [object maps] has [special support for OOP]({{rootUrl}}/language/object-m
|
||||
| [Object map] properties holding values | properties |
|
||||
| [Object map] properties that hold [function pointers] | methods |
|
||||
|
||||
When a property of an [object map] is called like a method function, and if it happens to hold
|
||||
a valid [function pointer] (perhaps defined via an [anonymous function]), then the call will be
|
||||
dispatched to the actual function with `this` binding to the [object map] itself.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
```rust
|
||||
// Define the object
|
||||
let obj = #{
|
||||
data: 0,
|
||||
increment: Fn("add"), // when called, 'this' binds to 'obj'
|
||||
update: Fn("update"), // when called, 'this' binds to 'obj'
|
||||
action: Fn("action") // when called, 'this' binds to 'obj'
|
||||
};
|
||||
|
||||
// Define functions
|
||||
fn add(x) { this.data += x; } // update using 'this'
|
||||
fn update(x) { this.data = x; } // update using 'this'
|
||||
fn action() { print(this.data); } // access properties of 'this'
|
||||
let obj =
|
||||
#{
|
||||
data: 0,
|
||||
increment: |x| this.data += x, // when called, 'this' binds to 'obj'
|
||||
update: |x| this.data = x, // when called, 'this' binds to 'obj'
|
||||
action: || print(this.data) // when called, 'this' binds to 'obj'
|
||||
};
|
||||
|
||||
// Use the object
|
||||
obj.increment(1);
|
||||
|
Reference in New Issue
Block a user