Search code examples
javascriptjavascript-objectsarrow-functions

JS "SyntaxError: expected expression, got '>'" with Arrow function within object


So, I'm trying to assign a arrow function to a variable within an object. Whenever I do this, it errors saying unexpected syntax. Are we not meant to assign arrow functions to variables within an object?

class contentController{
    constructor(tabs,tree){
        this.tree=tree;
        this.tabs=tabs;
        this.tabs.addCallback(id=>this.clickTab(id));
        this.tree.addCallback(id=>this.clickTree(id));
        this.currentId=null;
        this.currentTab=null;
        this.properties=document.getElementById('category_props');
        this.products=document.getElementById('category_items');
        this.propform=document.getElementById('categorypropform');
        this.events={
            clickReset:(e)=>{this->clickReset(e)}, //does not like!
            clickUpdate:(e)=>{this->clickUpdate(e)} //this either
        }
        this.events.clickUpdate=clickUpdate;
        this.events.clickReset(1);
    }

    //class continues for many, many lines...
}

Solution

  • You have some syntax error, in arrow function you would need to always use =>, and the way to access to a property of an element, such as method(functions) is with the dot notation

       class contentController{
            constructor(tabs,tree){
                this.tree=tree;
                this.tabs=tabs;
                this.tabs.addCallback(id=>this.clickTab(id));
                this.tree.addCallback(id=>this.clickTree(id));
                this.currentId=null;
                this.currentTab=null;
                this.properties=document.getElementById('category_props');
                this.products=document.getElementById('category_items');
                this.propform=document.getElementById('categorypropform');
                this.events={
                    clickReset:(e)=>{this.clickReset(e)}, 
                    clickUpdate:(e)=>{this.clickUpdate(e)} 
            }
                this.events.clickUpdate=clickUpdate;
                this.events.clickReset(1);
            }
            //class continues for many, many lines...
        }