TypeScript example

TypeScript example

HTML

<div id="foobar">
  <h2>Foo</h2>
  <button type="button">Bar</button>
</div>

TypeScript

$(() => {
	var foo = new Bar($("#foobar"));
})

class Bar {
    private view: JQuery;
    private button: JQuery;
    
    constructor(view: JQuery) {
    		// Fields
        this.view = view;
        this.button = view.find("button");
        
        // Events
        this.button.click(() => { this.buttonClick() });
    }
    
    private buttonClick() {
    	this.view.find("h2").css("background-color", "red");
    }
}