Javascript 函数有子函数/变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4906675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:19:12  来源:igfitidea点击:

Javascript function have sub functions / variables

javascriptfunctionobject

提问by Ragnis

This is the working code:

这是工作代码:

var test = function ()
{
    console.log(test.data);
};

test.data = 'hello';

test.set = function (data)
{
    test.data = data;
};

test.set('Test');
test();

This outputs Testto my javascript console. Now I was wondering, if there was a way to do it using something like this?

这输出Test到我的 javascript 控制台。现在我想知道,是否有办法使用这样的方法来做到这一点?

var test = {
    this: function ()
    {
        console.log(test.data);
    },

    data: 'hello',

    set: function (data)
    {
        test.data = data;
    }
};

采纳答案by Felix Kling

As I have written in my comment, you cannot make an object "callable". You can however automate the process from your first example:

正如我在评论中所写的那样,您不能使对象“可调用”。但是,您可以从第一个示例中自动执行该过程:

function extend(func, props) {
    for(var prop in props) {
        if(props.hasOwnProperty(prop)) {
            func[prop] = props[prop];
        }
    }
    return func;
}

and then call it with:

然后调用它:

var test = extend(function(){
    console.log(test.data);
},
{
    data: 'hello',    
    set: function (data) {
        this.data = data;   // note that I changed it to `this.data`
    }
});

DEMO

演示



That said, I think you should not use functions like that. It will be easier to understand if you just have a "normal" object and call every method with obj.method()instead of having obj().

也就是说,我认为你不应该使用这样的函数。如果您只有一个“普通”对象并使用obj.method()而不是使用obj().

At least you have to document this very carefully.

至少你必须非常仔细地记录下来。

回答by Arne

How about doing something like this:

如何做这样的事情:

function Test () {
  this.data = 'hello';
  this.set = function (data)
    {
        test.data = data;
    }
  this.log = function ()
    {
        console.log(test.data);
    }
}

var test = new Test ();
test.set('Test');
test.log();

This has the advantage you can create new instances easily.

这具有您可以轻松创建新实例的优势。



If you just want a one-off, I would say your own suggestion is almost what you want:

如果你只是想要一次性,我会说你自己的建议几乎就是你想要的:

var test = {
    log: function ()
    {
        console.log(test.data);
    },

    data: 'hello',

    set: function (data)
    {
        test.data = data;
    }
};

test.set('Test');
test.log();

But perhaps your question was how to avoid the ".log" part?

但也许您的问题是如何避免“.log”部分?

回答by tdobek

You can store any functions under properties in your object. And you can invoke them:

您可以在对象的属性下存储任何函数。你可以调用它们:

let f = { fun1: function () 
                {
                     return 1; 
                } 
        };
f.fun1();

is going to work perfectly. I am not sure if you can use 'this' as a property name as it is a keyword. Probably no problem with that, but it might be misleading.

将完美地工作。我不确定您是否可以使用“this”作为属性名称,因为它是一个关键字。可能没有问题,但它可能会产生误导。