静态函数声明和Java中普通函数声明之间的区别? [英] Difference between Static function declaration and the normal function declaration in Javascript?

查看:92
本文介绍了静态函数声明和Java中普通函数声明之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用多种方法在javascript中声明函数. 一种方法是在内部声明一个类和一个静态函数,如下所示.

There are many ways one can declare a function in javascript. One of the ways is declaring a class and a static function inside is as showed below.

class className {
 static fucntionName() {
 }
}

另一种声明方式是通过如下所示的传统javascript样式.

another way of is declaring is through the tradition javascript style as showed below.

export function functionName() {
}

我想知道使用这两种情况的优缺点. 静态方法是否有任何特定的用例,为什么要声明一个类(我们知道在javascript中,无需实例化该类即可访问静态函数). 为什么不在所有用例中都使用传统的函数声明方式(上例中的第二种情况)?

I would like to know the advantages/disadvantages of using either of the cases. Is there any specific use cases for the static methods, why declare a class(we know that in javascript there is no need to instantiate the class in order to access the static function). Why not just use the traditional way (the second case in the above example) of function declaration in all/any use case?

我想详细了解这一点.

I would like to understand this is in detail.

推荐答案

我想知道使用这两种情况的优缺点.

I would like to know the advantages/disadvantages of using either of the cases.

苹果和橘子.不必导出类,模块也不必是类.尽管导出的模块可以是一个类,但它们不是一回事.

apples and oranges. A class doesn't have to be exported and a module doesn't have to be a class. They are not the same thing, although the exported module can be a class.

静态方法是否有特定的用例

Is there any specific use cases for the static methods

当您想要一个类提供某些功能时,但这并不是该类的单个实例所特有的.像var c = Point.interpolate(a, b, .5);
静态interpolate函数不属于其正在操作的任何点.在提供此功能(内插点) 的类上静态定义此函数更合适.

When you want a class to provide some functionality, but this aint' something that is specific to a single instance of that class. Like var c = Point.interpolate(a, b, .5);
The static interpolate function doesn't belong to any of the points it's operating on. It's more appropriate to define this function static on the class that provides this functionality (interpolating points).

我们知道,在javascript中,无需实例化该类即可访问静态函数

we know that in javascript there is no need to instantiate the class in order to access the static function

这就是静态函数的定义,您无需实例化类即可调用它.那不是JS特有的.

That's the very definition of a static function that you don't have to instantiate the class to call it. That's not something JS specific.

为什么不在所有情况下都使用函数声明的传统方式(上例中的第二种情况)

Why not just use the traditional way (the second case in the above example) of function declaration in all/any use case

因为我刚开始,一个类和一个模块不一定是同一件事.而且写起来更好/更干净

Because as I started, a class and a module ain't necessarily the same thing. And it is nicer/cleaner to write

class Foo{
    ...
    static fn(){...}
}

class Foo {
    ...
}

Foo.fn = function(){...};

尽管最后,两者都只不过是

Although in the end, both is just little more than

function Foo(){...}
Foo.fn = function(){};
// + Foo.prototype stuff

但是类语法更简洁.即使您只是在扫描这段代码,也更易于阅读和理解.

But the class syntax is cleaner. Simpler to read and to understand, even if you're just scanning over this piece of code.

这篇关于静态函数声明和Java中普通函数声明之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆