Class

Class

Class

Function

class is used to encapsulate a set of fields and methods into the same object structure. Fields save instance status, methods describe instance behavior, and this is used internally to access the current instance.

BT's class syntax follows a JavaScript-like style, but constructs instances with ClassName::constructor(...). The most common constructor name is new, so this is usually written as User::new(...).

Syntax

Define class

Create instance

Class::new() is the current instantiation writing method of BT, not new Class().

Members

class members are divided into fields and methods. The

Fields use name: expression declarations:

Naked field The

field can also only write the name without writing : and initial value. This writing method is called a naked field. The naked field will occupy a member name in the instance in advance, and the default value is empty.

The naked fields name and name: '' are different: the default value of name is empty, and the default value of name: '' is the empty string. this.name = name is usually used in the constructor to override the default value. The

Methods do not use fn; write method_name(parameters) { ... } directly:

method has the same return value as an ordinary function. When there is no explicit return, the value of the last statement in the method body is the return value.

this

this represents the currently operating instance and is only used in class methods and constructors.

this.value is used to read the current instance fields, this.value = 1 is used to write the current instance fields, and this.method() is used to call other methods of the current instance.

Access to private members must be done directly through this.xxx. Do not assign this to other variables before accessing private members, because permission judgment only regards access directly from this as class internal access.

new

new is the most common constructor name. Write ClassName::new(...) when creating an instance.

If new is not defined in the class, Class::new() will still create an instance and use the default value in the class field:

The constructor return rule requires special attention: the BT method returns the value of the last statement by default. After the constructor is executed, if empty or null is returned, Class::new() will return a new instance; if another value is returned, that value will be returned.

Therefore, it is recommended to write this in the last line of the constructor to clearly return the current instance:

Don't just put the assignment statement in the last line of the constructor:

The last statement of new above is an assignment expression, and the assignment expression returns 'Tom' on the right, so User::new('Tom') returns a string, not an instance.

Multiple structure entries

BT constructors are not limited to new. Any public method can be used as a construction entry through ClassName::method_name(...): the runtime first creates an instance, then binds this to it while executing that method.

Except for new, the method used as the entrance to the construction needs to be pub, otherwise it cannot be accessed outside the class.

Public and private

class members are private by default. Only fields or methods added with pub can be accessed through instances outside the class.

Externally accessible:

Externally not accessible:

Private members are still accessible inside the class via this:

BT currently does not have the private keyword. The default is private, write pub only when it needs to be made public.

Field writing

Public fields can be read and written outside the class:

Private fields can only be written inside the class through this:

If a field that has not been declared in the class definition is added to this, this field will be saved as a private field; if a field is added on an external instance, this field will be saved as a public field.

The above token is a private field added through this inside the class, and nickname is a public field added externally.

Chain call

BT supports chain call. If the class method wants to continue the chain call, just return this.

Parameters

class itself has no calling parameters. Parameters are written on the constructor or ordinary method.

Return Value

  • class Name { ... } will create a class value and save it to the class name variable.
  • Name::new(...) returns a class instance by default.
  • Name::method(...) will first create a class instance, and then execute the specified method as the construction entry.
  • The instance method returns the value of the last statement in the method body, or you can use return to force the return.
- The constructor call returns a new instance when the constructor method returns empty or null; it returns that value when another value is returned.

Code Examples

Complete encapsulation example

Private member protection status

Notes

  • Class::new() is instantiation syntax; do not write new Class().
- new is the recommended constructor name; if new is not defined, Class::new() will also return an instance with default fields.
  • It is recommended to write this in the last line of the construction method to prevent the last assignment statement from returning ordinary values such as strings and numbers as the construction result.
  • Members are private by default; public fields or methods must be written as pub.
- Private members can only be accessed inside the class via this.xxx.
  • There is currently no private, extends, super, static syntax; do not use it according to the JS class inheritance writing method.
  • Field initial values are suitable for scalar default values; variable values such as arrays and objects are recommended to be initialized with this.xxx = [] or this.xxx = {} in new to avoid multiple instances sharing the same default reference.