OOP support.
This commit is contained in:
44
doc/src/language/oop.md
Normal file
44
doc/src/language/oop.md
Normal file
@@ -0,0 +1,44 @@
|
||||
Object-Oriented Programming (OOP)
|
||||
================================
|
||||
|
||||
{{#include ../links.md}}
|
||||
|
||||
Rhai does not have _objects_ per se, but it is possible to _simulate_ object-oriented programming.
|
||||
|
||||
|
||||
Use [Object Maps] to Simulate OOP
|
||||
--------------------------------
|
||||
|
||||
Rhai's [object maps] has [special support for OOP]({{rootUrl}}/language/object-maps-oop.md).
|
||||
|
||||
| Rhai concept | Maps to OOP |
|
||||
| ----------------------------------------------------- | :---------: |
|
||||
| [Object maps] | objects |
|
||||
| [Object map] properties holding values | properties |
|
||||
| [Object map] properties that hold [function pointers] | methods |
|
||||
|
||||
|
||||
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'
|
||||
|
||||
// Use the object
|
||||
obj.increment(1);
|
||||
obj.action(); // prints 1
|
||||
|
||||
obj.update(42);
|
||||
obj.action(); // prints 42
|
||||
```
|
Reference in New Issue
Block a user