⌚SB Watch 2
🚚 Free fedex shipping from 97 $
🌎 WORLDWIDE SHIPPING for 25 $
Search
Close this search box.

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