首页 > 解决方案 > JS“语法错误:预期的表达式,得到'>'”对象内带有箭头函数

问题描述

所以,我试图将箭头函数分配给对象中的变量。每当我这样做时,它都会错误地说出意外的语法。我们不打算将箭头函数分配给对象内的变量吗?

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

标签: javascriptjavascript-objectsarrow-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...
    }

推荐阅读