简体   繁体   English

Javascript 中静态函数声明和普通函数声明的区别?

[英]Difference between Static function declaration and the normal function declaration in Javascript?

There are many ways one can declare a function in javascript.可以通过多种方式在 javascript 中声明函数。 One of the ways is declaring a class and a static function inside is as showed below.一种方法是声明一个类,里面有一个静态函数,如下所示。

class className {
 static fucntionName() {
 }
}

another way of is declaring is through the tradition javascript style as showed below.另一种声明方式是通过传统的 javascript 样式,如下所示。

function functionName() {
}

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).静态方法是否有任何特定用例,为什么要声明一个类(我们知道在 javascript 中不需要实例化类来访问静态函数)。 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静态方法是否有任何特定用例

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);var c = Point.interpolate(a, b, .5);
The static interpolate function doesn't belong to any of the points it's operating on.静态interpolate函数不属于它所操作的任何点。 It's more appropriate to define this function static on the class that provides this functionality (interpolating points) .在提供此功能(插值点)的类上定义此函数静态更合适。

we know that in javascript there is no need to instantiate the class in order to access the static function我们知道在 javascript 中不需要实例化类来访问静态函数

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.这不是特定于 JS 的东西。

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(){...}
}

than

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.更易于阅读和理解,即使您只是浏览这段代码。

A static method is callable from the class itself, as you already know.如您所知, static方法可从类本身调用。

A non- static method is callable from an instance of the class, so you basically have to create an object before being able to access that method.可以从类的实例调用非static方法,因此您基本上必须先创建一个对象,然后才能访问该方法。

For example, for a addNumbers(var a, var b) which does return a+b , is it really necessary to waste memory instantiating an object of the class just to add those 2 numbers?例如,对于确实return a+baddNumbers(var a, var b) ,是否真的有必要浪费内存实例化类的对象只是为了添加这两个数字? No, you just need the result and that's the whole point of having static.不,您只需要结果,这就是静态的全部意义所在。

Using the first style allows you to group methods in a particular class (think of something like namespaces).使用第一种样式允许您对特定类中的方法进行分组(想想命名空间之类的东西)。 Maybe you can define classes like Math and String , which both have the add method but implemented in a different way.也许你可以定义像MathString这样的类,它们都有add方法,但以不同的方式实现。 Calling add() by itself would be confusing, but Math.add() and String.add() are not.单独调用add()会令人困惑,但Math.add()String.add()不会。

The export style, on the other way, does a completely different thing.另一方面, export样式做的是完全不同的事情。 It allows you to use functions from another module.它允许您使用来自另一个模块的函数。

Think about this:想一想:

first_module.js first_module.js

function cube(var x) {
    return x * x * x;
}

second_module.js第二个模块.js

import { cube } from 'first_module'; // <-- ERROR
alert( cube(3) ); // <-- Undefined function

But, if you declare first_module this way:但是,如果您以这种方式声明 first_module:

export function cube(var x) {
    return x * x * x;
}

Then second_module will work fine.然后 second_module 将正常工作。

What is a static function?什么是静态函数?

Declaring a static function within a class creates a class level function that is not callable by instances of that function.在类中声明静态函数会创建一个类级别的函数,该函数的实例无法调用该函数。 An example of this is the Object.assign function. Object.assign函数就是一个例子。 Static functions are often used to build functions that operate on instances of the type on which it is defined.静态函数通常用于构建对定义它的类型的实例进行操作的函数。

A static method can only be called within the class and is useful for utility functions, like say, massaging the parameter variables that are coming in, or setting up constants, (or Object.assign mentioned previous) when that must be done every time, before passing on to an instantiation of the class, to avoid unnecessary use of memory as a previous commentator noted.静态方法只能在类中调用,并且对于实用函数很有用,例如,在每次都必须执行时,按摩传入的参数变量或设置常量(或前面提到的 Object.assign),在传递到类的实例化之前,避免不必要地使用内存,正如前面的评论员指出的那样。

Once everything is cleaned up, you can export the rest of your function for instantiation elsewhere in your code, where arguments will be cleaned up/constants set, again and again each time you use the class, and your instantiation can focus on the dynamic stuff.清理完所有内容后,您可以导出函数的其余部分以在代码中的其他地方进行实例化,每次使用该类时都会一次又一次地清理参数/设置常量,并且您的实例化可以专注于动态内容.

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

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