Javascript 从对象文字中设置函数属性

Javascript 从对象文字中设置函数属性,javascript,node.js,ecmascript-6,Javascript,Node.js,Ecmascript 6,我正在声明任务对象: var tasks = { test: () => { /// .... } }; 在test函数中,我想设置tasks.test.description属性。到目前为止,我已经尝试: var tasks = { test: () => { // need to set tasks.test.description here // // tried without s

我正在声明
任务
对象:

var tasks = {

    test: () => {
        /// ....
    }

};

test
函数中,我想设置
tasks.test.description
属性。到目前为止,我已经尝试:

var tasks = {
    test: () => {
        // need to set tasks.test.description here
        // 
        // tried without success:
        // tasks.test.description = '...';
        // this.description = '...';
        // arguments.callee.description = '...';
    }
};
以及:

var tasks = {
    test: function xxx() {
        // all methods from example above, plus:
        // xxx.description = '...';
    }
};
当从功能范围外访问时,描述始终处于未定义状态

console.log(tasks.test.description); // => undefined

有没有一种方法可以在对象文本的函数定义中设置description属性?

您的第一种方法几乎是正确的,但是您必须调用函数才能执行任何操作

var任务={
测试:()=>{
tasks.test.description='…';
}
};
tasks.test();

log(“tasks.test.description的值为”+tasks.test.description)生成console.log(tasks.test.description)的原因;返回未定义的是您定义的对象,但在首次运行tastks.test()方法之前,将创建description属性。要将属性描述添加到对象方法,请尝试以下操作:

const tasks = {
  test() {
    //....
  }
};

tasks.test.description = 'asd';


你的一次尝试几乎是对的。您只需调用
tasks.test()
即可设置
tasks.test.description

var任务={
测试:函数xxx(){
xxx.description='…';
}
};
tasks.test();

日志(tasks.test.description)可能使用Object.assign将函数与对象组合:

 test: Object.assign(() => {
    /// ....
  }, {
   description: "whatever"
 })

要了解您正在做什么,请发布一个
tasks.test.description
方法。你知道函数中的代码只是在函数被调用时运行的吗?请你澄清一下你想做什么@Ele我试图声明一个对象,并将
test
属性设置为函数,并将该函数的
description
属性设置为字符串。我想通过使用一条语句(即上面粘贴的对象声明)来实现这一点。如果这是不可能的,我可以接受,不需要因为答案是“否”而进行否决表决。等等,OP声明:在测试函数中,我想设置tasks.test.description属性。这是如何回答这个问题的?@ele OP想要什么是不可能的。我完全同意你的看法,但是,这个答案没有帮助。我认为,你可以把这些信息放在这里,并参考这个备选方案。这似乎是最有创意的答案,尽管我的问题可能有点不准确。呵呵呵呵,不清楚的问题导致不清楚的公认答案
:)
。。。祝您今天过得愉快!!!
 test: Object.assign(() => {
    /// ....
  }, {
   description: "whatever"
 })