简体   繁体   English

编写JavaScript函数的更好方法

[英]Better way for writing JavaScript function

The following ways of writing a javascript function are equivalent. 以下编写javascript函数的方法是等效的。

Maybe the first one is more clear. 也许第一个更清楚。

Nevertheless, many programmers prefer the second way. 尽管如此,许多程序员还是喜欢第二种方式。

There are significant difference between the two ways for preferring the second-one? 偏爱第二种方式的两种方式之间存在显着差异。

First way: 第一种方式:

Class.prototype.fn = function () {
        var obj = { 
            … 
        };

        return obj;
};

Second way: 第二种方式:

Class.prototype.fn = function () {

        return {
            .........
          };
};

Unless you need to perform an operation on obj after creating it via the literal, there's no difference and it's just a subjective style preference. 除非您通过文字创建了obj之后需要对obj执行操作,否则没有什么区别,它只是一个主观的样式首选项。 (Note that said use could be in the code, or during debugging; more below.) (请注意,上述用法可以在代码中或调试期间使用;更多内容请参见下文。)

So for example, there's a real practical difference here: 因此,例如,这里有一个实际的实际差异:

Class.prototype.fn = function () {
        var obj = { 
            subordinate: {
                foo: function() {
                    if (obj.flag) {
                        doOneThing();
                    }
                    else {
                        doSomethingElse();
                    }
                }
            }
        };

        return obj;
};

There, you need the name, so that obj.subordinate.foo() works. 在那里,您需要名称,以便obj.subordinate.foo()起作用。 (Not that I'd advocate doing this, but it's an example of when there's an objective rather than subjective distinction.) But barring needing to use it after initialization and before return, it's just a subjective thing. (并不是我提倡这样做,但这只是一个有客观区别而不是主观区别的示例。)但是除非在初始化之后和返回之前需要使用它,否则这只是主观的事情。

Of course, that use need not necessarily be in the code . 当然,该用法不必一定在代码中 The form with the obj variable can be more useful when debugging, if you need to inspect what you're returning before you return it. 如果需要在返回之前检查返回的内容,则带有obj变量的表单在调试时会更有用。


Perhaps going a bit off-piste here, but I think it's related: In contrast to the examples in your question, there's a real, practical, tangible difference between this: 也许这里有点滑雪道,但是我认为这是相关的:与您问题中的示例相反,两者之间存在真正,实际,切实的区别:

Class.prototype.foo = function () {
        … 
};
Class.prototype.bar = function () {
        … 
};

and this: 和这个:

(function() {
    function Class_foo() {
            … 
    }
    function Class_bar() {
            … 
    }

    Class.prototype.foo = Class_foo;
    Class.prototype.bar = Class_bar;
})();

...that difference being that in the former, the functions have no names (the properties referring to them do, but not the functions themselves). ...区别在于前者中的函数没有名称(引用它们的属性具有名称,但函数本身没有名称)。 In the latter case, the functions have real names , which help your tools help you by showing you names in call stacks, lists of breakpoints, etc. 在后一种情况下,这些函数具有实名 ,这可以通过在调用堆栈,断点列表等中显示名称来帮助您的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM