C# 和 Javascript 中的函数之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11061444/
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-26 11:59:09  来源:igfitidea点击:

Difference between functions in C# and Javascript?

c#javascript

提问by JD.

I am learning Javascript and I have read functions are first class objects and most authors mention that functions can return functions (which are objects) and pass them as parameters to other functions.

我正在学习 Javascript 并且我读过函数是一流的对象,大多数作者提到函数可以返回函数(它们是对象)并将它们作为参数传递给其他函数。

I am sure there is a lot more to it so what are the differences between functions in C# and functions in javascript?

我相信它还有很多,那么 C# 中的函数和 javascript 中的函数之间有什么区别?

In C# am I right to say functions are not objects (don't have methods, properties etc) even though with closures (with lambda expressions and delegates) they seem to behave like function objects as in javascript?

在 C# 中,我说函数不是对象(没有方法、属性等)是否正确,即使使用闭包(使用 lambda 表达式和委托)它们似乎表现得像在 javascript 中的函数对象?

I feel that with lambda expressions in C# the distinction becomes a bit blurry for someone just coming to the language.

我觉得对于 C# 中的 lambda 表达式,对于刚接触该语言的人来说,区别变得有点模糊。

采纳答案by user541686

The onebiggestdifference I can think of is that in C# functions, variables are lexicallyscoped, whereas Javascript, variables are lexically scoped exceptfor this, which is dynamicallyscoped.

一个最大的,我能想到的区别是,在C#中的函数,变量是词法范围的,而JavaScript中,变量都是局部的,除了this,这是动态范围的。

For example, in Javascript, you can say something like

例如,在 Javascript 中,您可以这样说

var foo = new Object();
foo.x = 0;
var bar = function() { this.x = 2; };  // first-class function... what's "this"?
foo.baz = bar;   // Now foo has a method called 'baz', which is 'bar'.
foo.baz();
alert(foo.x);   // Displays 2 -- 'this' magically refers to 'foo'!

Try writing this in C#.

尝试用 C# 编写它。

Or actually, don't bother -- it will not make any sense.

或者实际上,不要打扰 - 它没有任何意义。

Why? Because thisdoesn't refer to anything here, lexically! Its meaning changes dynamically, unlike in C#, where its meaning simply depends on the enclosing class, not who the caller actually is.

为什么?因为this在词汇上不指代任何东西!它的含义是动态变化的,这与 C# 不同,在 C# 中,它的含义仅取决于封闭类,而不是调用者实际是谁。

回答by jcolebrand

What most authors say is that "functions are first classcitizens" in javascript. They are notfirst class in C#, in that they are merely methodsthere, attached to a class. Method delegates (and lambdas) are specific classes for the purpose of making methods likefirst class citizens.

大多数作者说的是javascript中的“函数是一等公民”。它们不是C# 中的第一类,因为它们只是那里的方法,附加到类。方法委托(和 lambdas)是特定的类,目的是使方法一等公民一样。

So here's the thing, as best I can explain it, without telling you to go back and read Crockford's "Javascript: The Good Parts" and something by like Skeet or someone:

所以这就是事情,尽我所能解释它,没有告诉你回去阅读 Crockford 的“Javascript:好的部分”和像 Skeet 或其他人这样的东西:

Javascript has a lot less parts than C#. Don't compare something you can do in C# directly with something you can (or can't) do in Javascript (and vice-verse).

Javascript 的部分比 C# 少得多。不要直接将您在 C# 中可以做的事情与您在 Javascript 中可以(或不能)做的事情(反之亦然)进行比较。

So why don't all languages (or at least C#) have "first class functions"? The answer is usually the simplest: Because that wasn't a language design requirement. That's sort of a cop-out on my part, however, because obviously they've added it in now. But if nobody told you to include it in your future-language, would you?

那么为什么不是所有的语言(或者至少是 C#)都具有“一流的功能”呢?答案通常是最简单的:因为那不是语言设计要求。然而,这对我来说是一种逃避,因为显然他们现在已经添加了它。但是如果没有人告诉你将它包含在你未来的语言中,你会吗?

So the key thing to remember is: in C# functions (methods) have to be attached to a class. In Javascript they don't. The why is in the language definition (spec).

所以要记住的关键是:在 C# 中,函数(方法)必须附加到一个类中。在 Javascript 中,他们没有。原因在语言定义(规范)中。

Also, I should follow this up with a statement on object creation:

另外,我应该用关于对象创建的声明来跟进:

You can create a new object in javascript in one of two ways:

您可以通过以下两种方式之一在 javascript 中创建新对象:

//using object syntax
var ob1 = { name: value, nestedFunction: function(){} };

or

或者

//using a function
function MyFunc(){
  var name = value;
  function nestedFunction(){}
}
var ob2 = new MyFunc();

And in the second case we're using a function to declare the object, in the first case we're just declaring it using literal syntax.

在第二种情况下,我们使用函数来声明对象,在第一种情况下,我们只是使用文字语法声明它。

To do the same thing in C# we have to create a new object as a class:

要在 C# 中做同样的事情,我们必须创建一个新对象作为一个类:

MyClass ob1 = new MyClass(); //constructor is a member function of the class
public class MyClass {
  public MyClass() {
  }
  public void nestedFunction(){
  }
}

Notice how in the Javascript one I still had to define the method that was being returned.

请注意在 Javascript 中我仍然必须定义返回的方法。

Ok, I think that's enough for tonight.

好的,我想今晚就够了。

回答by Kevin McKelvin

Javascript functions can be created and assigned to a variable, they're first class citizens in the language and have some more flexibility over the syntactical sugar that C# has applied.

可以创建 Javascript 函数并将其分配给变量,它们是语言中的一等公民,并且比 C# 应用的语法糖具有更大的灵活性。

In C#, when you write the code

在 C# 中,当您编写代码时

x => x * x

x => x * x

The compiler creates a real named method and wraps that method in a delegatethat can be executed.

编译器创建一个真正的命名方法并将该方法包装在一个delegate可以执行的方法中。

Delegates, along with the syntactic sugar that C# applies gives a very similar feel to JavaScript, but it's doing very different things internally.

委托以及 C# 应用的语法糖给人一种与 JavaScript 非常相似的感觉,但它在内部做着截然不同的事情。

In JavaScript you can create a function and assign it directly to a variable and execute it without wrapping it in any other structures to enable the action. Functions are first class citizens in JavaScript.

在 JavaScript 中,您可以创建一个函数并将其直接分配给一个变量并执行它,而无需将其包装在任何其他结构中以启用该操作。函数是 JavaScript 中的一等公民。

回答by theNorthFish

I have some rough ideas about the difference between JS and C#:

我对 JS 和 C# 之间的区别有一些粗略的想法:

1, Js is an interpreted language, which means JS needs a browser build-in ECMAScript interpreter to work. Whereas C# is a Compiled language, which means C# codes will be compiled as IL to work.

1、Js是一种解释型语言,也就是说JS需要浏览器内置的ECMAScript解释器才能工作。而 C# 是一种编译语言,这意味着 C# 代码将被编译为 IL 才能工作。

2,JS is a dynamically typed language, which means you don't need to specify the types of the variables when defining. Whereas C# is statically typed language, which means you need to specify the exact types of the variables.

2,JS是动态类型语言,定义时不需要指定变量的类型。而 C# 是静态类型语言,这意味着您需要指定变量的确切类型。

3, OOP-encapsulation, in JS, a lot of people say function is first class citizen. Normally, function has 2 kinds of usages: a)just a function to manipulate some work b)class&constructor, using which you can instantiate objects. Whereas in C#, function is function, which should belong to class(or interface).

3、OOP-encapsulation,在JS中,很多人说函数是一等公民。通常,函数有两种用法:a) 只是一个操作某些工作的函数 b) 类和构造函数,您可以使用它来实例化对象。而在 C# 中,函数就是函数,它应该属于类(或接口)。

4, OOp-inheritance, in JS, we use prototype to realize inheritance. Whereas in C#, we strictly use class to realize inheritance.

4、OOp-inheritance,在JS中,我们使用prototype来实现继承。而在 C# 中,我们严格使用类来实现继承。

5, OOP-polymorphism, in JS, according to my knowledge, we can use arguments, which is a pseudo-array, to realize functional overload to simulate polymorphism, which is a bruising. Whereas, for C#, polymorphism is so perfectly embodied.

5、OOP-多态,在JS中,据我所知,我们可以用arguments,也就是一个伪数组,来实现函数重载来模拟多态,这是个坑。而对于 C# 来说,多态性得到了完美的体现。

6, In JS, we don't have GC. Whereas, there is a strong GC for C#.

6、在JS中,我们没有GC。然而,C# 有一个强大的 GC。

Maybe some ideas are not correct, welcome suggestions.

也许有些想法不正确,欢迎提出建议。

回答by Mike Bahar

Just to clarify some confusion;

只是为了澄清一些混乱;

1) JavaScript is a compiled language indeed. The way the code is compiled is right before the execution on the browser, and it is fast! This is why some developers think it's an 'interpreted' language.

1) JavaScript 确实是一种编译语言。代码的编译方式正好在浏览器上执行之前,而且速度很快!这就是为什么一些开发人员认为它是一种“解释型”语言。

2) C# does not have functions in JS sense. It has class methods.

2)C#没有JS意义上的函数。它有类方法。