JS“ SyntaxError:期望的表达式,在对象内带有箭头功能”

时间:2018-07-21 18:38:00

标签: javascript javascript-objects arrow-functions

因此,我正在尝试将箭头功能分配给对象内的变量。每当我这样做时,都会说出意外的语法错误。我们不是要向对象内的变量分配箭头功能吗?

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...
}

1 个答案:

答案 0 :(得分:1)

您有一些语法错误,在箭头函数中,您需要始终使用=>,并且访问元素属性(例如method(functions))的方法是使用点符号

   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...
    }