Difference between revisions of "New"
Jump to navigation
Jump to search
(Created page with "<c>new</c> is a keyword unary operator used to create a subclass or instance. <c>new</c> acts on a map, and returns a new map with __isa set to the opera...") |
(fix syntax) |
||
| (5 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | <c>new</c> is a [[ | + | <c>new</c> is a [[:Category:Keywords|keyword]] unary [[operators|operator]] used to create a subclass or instance. It is part of MiniScript's support for [[object-oriented programming]]. |
<c>new</c> acts on a map, and returns a new map with [[__isa]] set to the operand. | <c>new</c> acts on a map, and returns a new map with [[__isa]] set to the operand. | ||
| + | |||
| + | In MiniScript, the new keyword is a unary operator used for object-oriented programming. It creates either: | ||
| + | |||
| + | a new instance of an object, or | ||
| + | a subclass derived from another map/object. | ||
| + | |||
| + | When new is applied to a map, it returns a fresh map whose <c>__isa</c> field points to the original operand. That creates prototype-style inheritance. | ||
| + | |||
| + | <ms>Animal = {} | ||
| + | Animal.speak = function | ||
| + | print "..." | ||
| + | end function | ||
| + | |||
| + | Dog = new Animal | ||
| + | |||
| + | Dog.speak = function | ||
| + | print "Woof!" | ||
| + | end function | ||
| + | |||
| + | myDog = new Dog | ||
| + | myDog.speak | ||
| + | |||
| + | Dog.__isa == Animal | ||
| + | myDog.__isa == Dog</ms> | ||
| + | |||
| + | This inheritance chain lets MiniScript look up methods and properties through parent objects automatically. | ||
[[Category:Language]] | [[Category:Language]] | ||
[[Category:Keywords]] | [[Category:Keywords]] | ||
[[Category:Operators]] | [[Category:Operators]] | ||
| − | |||
| − | |||
Latest revision as of 03:24, 21 May 2026
new is a keyword unary operator used to create a subclass or instance. It is part of MiniScript's support for object-oriented programming.
new acts on a map, and returns a new map with __isa set to the operand.
In MiniScript, the new keyword is a unary operator used for object-oriented programming. It creates either:
a new instance of an object, or a subclass derived from another map/object.
When new is applied to a map, it returns a fresh map whose __isa field points to the original operand. That creates prototype-style inheritance.
Animal = {}
Animal.speak = function
print "..."
end function
Dog = new Animal
Dog.speak = function
print "Woof!"
end function
myDog = new Dog
myDog.speak
Dog.__isa == Animal
myDog.__isa == Dog
This inheritance chain lets MiniScript look up methods and properties through parent objects automatically.