Aussom has two kinds of variables: local and member. They look the same when you read them, but they differ in lifetime and in how you reach them.
A local variable is created by an assignment inside a function or a function argument list. It exists only while that function is running. Once the function returns, the variable is gone.
class Demo {
public main(args) {
// Three local variables, created right here.
state = "California";
elevation = 100.234;
items = ["apple", "pear"];
}
}
Function arguments are local variables too. When the caller passes values, those values are bound to the argument names for the duration of the call.
public greet(Name, Age) {
msg = "Hello " + Name + ", age " + Age;
c.log(msg);
}
Local variables are not declared with a type. Whatever you assign to them becomes their value, and reassigning is allowed:
public main(args) {
x = 10; // int
x = "ten"; // now a string - this is fine
}
A member variable lives on the object. It is declared in the class
body and persists for the lifetime of that instance. Inside the
class, reach it through this. From outside, reach it through the
instance.
class User {
public firstName = "";
public lastName = "";
public fullName() {
return this.firstName + " " + this.lastName;
}
}
class App {
public main(args) {
u = new User();
u.firstName = "Avery";
u.lastName = "Hogan";
c.log(u.fullName());
}
}
Output:
Avery Hogan
The this keyword is required when a member references another
member from inside the class. Writing firstName alone inside
fullName() would be looked up as a local, not a member.
public and privateMember declarations can be marked public or private.
public means callers outside the class can read or assign the
member, and any method on the class can call a public method.private means only methods on the same class can touch the
member.If you leave the modifier off, the member defaults to private.
class Account {
public balance = 0; // anyone can read or set
private salt = "x9k2"; // only methods on Account can use this
public deposit(int Amount) {
this.balance += Amount;
}
private hash(string Input) {
return Input + this.salt;
}
}
class App {
public main(args) {
a = new Account();
a.balance = 100; // OK
a.deposit(25); // OK
// a.salt = "y"; // error: salt is private
// a.hash("hello"); // error: hash is private
}
}
Note that local variables have no access modifiers. The keywords
public and private only apply to members and methods.
A member can be initialized with a literal expression in its declaration. Maps and lists are allowed, but only with primitive values - you cannot call functions or evaluate complex expressions in a member's default. To initialize from a function call, do it in the constructor.
class Profile {
// OK - primitive default values
public name = "";
public tags = ["new", "active"];
public settings = { theme: "dark", units: "metric" };
// Not allowed - default cannot call a function
// public createdAt = sys.now();
public Profile() {
// OK - free to call functions in the constructor
this.createdAt = sys.now();
}
}