Skip to content
This repository has been archived by the owner on Oct 23, 2018. It is now read-only.

Classes and singletons

dangreen edited this page Jan 3, 2015 · 5 revisions

Classes

Classes in ColaScript looks like classes from Dart:

  • Exists named contructors
  • It is not necessary to use this context
  • super context to call parent methods

But in ColaScript classes:

  • exists self alias of class context
  • you can "run" any code in class, it will be moved in constructors
class ListData {
    covert Array listData = [];
    static const String About = "Example of class";

    // it will be moved in constructors
    $("#btn").click(() => alert(About));

    ListData(Array data) {
        listData = data;
    }

    ListData.fromString(String text) {
        listData = text.split('\n');
    }

    Array get data() {
        console.log(About);
        return listData;
    }

    set data(Array data) {
        listData = data;

        $("#list-title").html(About);
        data.forEach((el){
            $("#list").append("<li>@el</li>");
        });
    }
}

console.log(ListData::About);

To not use this context for parents props you should redefine props.

export class DataList extends ListData {
    covert Array listData;
    static const String About;

    DataList.fromString(String text) {
        console.log(text);
        super.fromString(text);
    }
}

Singletones

singleton HttpRequest {

    // it will be moved in constructors
    GET("echo.php", (response) {
        console.log(response);
    });

    GET(String url, Function cb) {
        var xhr = new XMLHttpRequest();
		    
        xhr.onreadystatechange() {
           if (xhr.readyState != 4) return;
           if (xhr.status == 200) cb(xhr.response);
        }
		    
        xhr.open("GET", url, true);
        xhr.send();
    }
}

list of modificators:

  • static to define static members
  • covert to define unenumerable members
  • const to define constants
  • export to export class
  • async to async using of methods with Promises (read more)