Objects
Introduction
Objects are a fundamental feature of the language that allows programmers to represent complex data and organize it into logical structures. Objects in MagiScript are similar to objects in other programming languages like JavaScript, and they are essential for building more advanced programs.
What are Objects?
Objects are collections of key-value pairs that represent a set of related properties and methods. Each key in an object is a string that represents a property, and the value associated with each key can be any valid MagiScript expression. For example, an object representing a person might have properties like “name,” “age,” and “gender,” each with a corresponding value.
Objects can also have methods, which are functions that are associated with the object and can be called using the object’s name. Methods can be used to perform operations on the object’s data or manipulate the object in other ways.
Creating Objects
To create an object, you can use curly braces to define the object’s properties and values.Β For example:
let person = {
name: "John",
age: 25,
gender: "male",
sayHello: function() {
console.log("Hello, my name is " + this.name + ".");
}
};
person.sayHello(); // prints the hello message
In this example, we’ve created an object called person
with properties name
, age
, and gender
. We’ve also defined a method called sayHello
that prints a greeting to the console.
Accessing Object Properties
To access the properties of an object, you can use dot notation or square bracket notation.Β For example:
console.log(person.name); // Output: John console.log(person['age']); // Output: 25
In both cases, we’re accessing the value associated with the name
and age
properties of the person
object.
Using Reflect.ownKeys
MagiScript supports Reflect.ownKeys, which is a method that returns an array of all the keys (both properties and methods) defined on an object.Β For example:
let keys = Reflect.ownKeys(person);
for (let i = 0; i < keys.length; i++) {
console.log(keys[i]); // Outputs 'name', 'age', 'gender', and 'sayHello'
}
In this example, we’re using Reflect.ownKeys
to get an array of all the keys defined on the person
object, including both properties and the sayHello
method.
Conclusion
Objects are a powerful feature of MagiScript that allow you to represent complex data and organize it into logical structures. By understanding how to create and access object properties, and how to use Reflect.ownKeys
to get a list of an object’s keys, you can start building more advanced programs in MagiScript.
General
πΒ First Steps
MagiScript Editor
πΒ Basics
πΒ Running Your First Program
πΒ App Store
πΒ Atom Settings (Editor)
πΒ Debugging Techniques
Examples
πΒ Atom Time
ποΈ Time Practice
πΒ Atom Pi (Pi Revelations)
πΒ Atom Drum
ποΈ Atom Stack
πΒ Atom Square
πΒ Atom Level
πΒ Atom THMPR
πΒ Poker Hands
πΒ Keyboard Numeric
πΒ Keyboard NOKIA
πΒ Keyboard Cursor
πΒ Keyboard Media
πΒ Keyboard Custom
Input/Output
πΒ Buttons
πΒ Vibration Motor
πΒ RGB LED
πΒ Devices
πΒ PeekSmith
πΒ SB Watch
πΒ Bluetooth Keyboard
πΒ Bluetooth Mouse
πΒ Timers
πΒ Database
πΒ Events
πΒ System (exit, sleep, rand)
πΒ Objects (card, time)
Language
πΒ Summary
πΒ Comments
πΒ Variable Declarations
πΒ Operators
πΒ Control Flow
πΒ Functions
πΒ Numbers
πΒ Strings
πΒ Arrays
πΒ Objects
πΒ Uint8Array