简体   繁体   English

Javascript VS C#

[英]Javascript VS C#

Maybe a strange and green question, but 也许是一个奇怪而又绿色的问题

Is there anything C# can't do what javascript can... And considering JQuery? 有没有什么C#不能做什么javascript可以...并考虑JQuery?

except for the fact that one is clientside, and the other serverside? 除了一个是客户端,另一个是服务器端的事实? Or am I asking a very stupid question now? 或者我现在问一个非常愚蠢的问题?

EDIT: to be more specific: I mean web programming, and indeed maybe a more useful question is: 编辑:更具体:我的意思是网络编程,实际上可能是一个更有用的问题:

> What can I do client side that I can't do server side, and vice versa? >我可以做什么客户端,我不能做服务器端,反之亦然?

> Are there more reasons to use both languages if you keep "server/clientside" out of scope? >如果将“server / clientside”超出范围,是否有更多理由使用这两种语言?

> some developers avoid javascript. >一些开发人员避免使用javascript。 why? 为什么?

What can I do client side that I can't do server side, and vice versa? 客户端我能做什么,我不能做服务器端,反之亦然?

Client-side: Javascript runs in most browsers without a plugin. 客户端:Javascript在没有插件的大多数浏览器中运行。 C# requires a browser plugin like Silverlight. C#需要像Silverlight这样的浏览器插件。 Even though it's running on a client machine, Javascript can't read and write files there. 即使它在客户端计算机上运行,​​Javascript也无法在那里读写文件。 C# in Silverlight may be able to read and write files depending on the Silverlight version and what the client allows. Silverlight中的C#可能能够读取和写入文件,具体取决于Silverlight版本以及客户端允许的内容。 Both Javascript and C#/Silverlight can talk to remote servers. Javascript和C#/ Silverlight都可以与远程服务器通信。

Server-side: since you control this machine, you can do whatever you want - read files, write files, talk directly to databases, etc. Keep in mind there's nothing stopping you from running Javascript server-side. 服务器端:既然你控制了这台机器,你就可以做任何你想做的事情 - 读取文件,写文件,直接与数据库对话等等。请记住,没有什么能阻止你运行Javascript服务器端。 Check out node.js . 查看node.js。

Are there more reasons to use both languages if you keep "server/clientside" out of scope? 如果您将“server / clientside”超出范围,是否有更多理由使用这两种语言?

I wouldn't leave the execution environment out of your analysis. 我不会将执行环境从您的分析中删除。 If you absolutely need client-side interaction and can't guarantee C# will execute on the client, C# isn't practical. 如果您绝对需要客户端交互并且无法保证C#将在客户端上执行,则C#不实用。 Likewise, if your company runs Windows servers and doesn't want to install Javascript runtimes/compilers, you won't be able to use Javascript on the server. 同样,如果您的公司运行Windows服务器并且不想安装Javascript运行时/编译器,您将无法在服务器上使用Javascript。

some developers avoid javascript. 一些开发人员避免使用JavaScript。 why? 为什么?

Problems with Javascript in a browser are absolutely awful to debug. 浏览器中的Javascript问题对于调试来说非常糟糕。 You're running on a machine that's out of your control - the user may be running an obscure or ancient browser, they may be using anti-virus software that mucks with your Javascript, their browser plugins might muck with your Javascript. 你正在一台不受控制的机器上运行 - 用户可能正在运行一个模糊或古老的浏览器,他们可能正在使用与你的Javascript混淆的防病毒软件,他们的浏览器插件可能会破坏你的Javascript。 It's hard. 这个很难(硬。

This is the cost of doing business on someone else's machine, however. 然而,这是在别人的机器上做生意的成本。 If it was easy, a beautiful client-side experience would mean less. 如果它很容易,那么美丽的客户端体验意味着更少。 Solving hard problems isn't for everyone but it sure is appreciated when it's done well. 解决难题不适合所有人,但是当它做得好时肯定会受到赞赏。

I take it your real question is, if c# can do everything, why should you use javascript at all? 我认为你真正的问题是,如果c#可以做任何事情,你为什么要使用javascript? The answer here is performance, both perceived and real. 这里的答案是表现,无论是感知还是真实。 The trick here is that to use c# to do the DOM manipulation normally associated with javascript, a browser has to post back an extra http request to the server and tell the c# code what to do. 这里的技巧是使用c#来执行通常与javascript相关联的DOM操作,浏览器必须向服务器发回额外的http请求并告诉c#代码该做什么。 Lets talk about those extra requests. 让我们谈谈那些额外的请求。 Spread around a lot of users, they add up very quickly and play havoc on your server infrastructure. 它们分散在很多用户身上,非常快速地加起来并对您的服务器基础架构造成严重破坏。 The "real" performance issue is that now a lot of work has to happen on your server(s), instead of in your users' browsers. “真正的”性能问题是,现在很多工作必须在您的服务器上进行,而不是在用户的浏览器中进行。 The "perceived" performance issues is that, even if you have the server resources to easily handle all the additional http requests, you user now has to spend extra time waiting for latency incurred by those http round trips. “感知”性能问题是,即使您拥有服务器资源来轻松处理所有其他http请求,您现在也需要花费额外的时间来等待这些http往返所带来的延迟。

Both languages rely heavily on API's that were designed for different domains. 这两种语言都严重依赖于为不同域设计的API。

JavaScript was originally intended to run inside of a browser, so it makes heavy use of DOM API's as well as other in-browser operations such as AJAX. JavaScript原本打算在浏览器中运行,因此它大量使用DOM API以及其他浏览器内操作,如AJAX。 C# probably does not have good support for such API's as it was never intended to be executed directly inside a browser - although Silverlight may provide such operations since it is (in a way) a "C# Sandbox" inside of a browser. C#可能对这样的API没有很好的支持,因为它从未打算直接在浏览器中执行 - 尽管Silverlight可能提供这样的操作,因为它(在某种程度上)是浏览器内的“C#Sandbox”。

On the other hand, C# is a general-purpose language that was designed to build basically any application, from server-side engines to client applications to services - you get the idea... 另一方面,C#是一种通用语言,旨在构建基本上任何应用程序,从服务器端引擎到客户端应用程序再到服务 - 你明白了......

I have seen a C# project where javascript is embedded and can execute javascript within a C# code. 我见过一个嵌入了javascript的C#项目,可以在C#代码中执行javascript。 Have a look here on CodeProject to see how that is achieved. 看看CodeProject ,看看它是如何实现的。

Technically, no. 从技术上讲,没有。

You could even use Javascript server-side if you wanted (or client-side C# via different mechanisms). 如果需要,您甚至可以使用Javascript服务器端(或通过不同的机制使用客户端C#)。

They're really just two ways of getting the same job done. 他们真的只是完成同样工作的两种方式。

the main difference is that C# (if speaking about web) is executed on server and JS on client . 主要区别在于C#(如果谈到web)是在服务器上执行的,而在客户端上是JS。 You can't execute C# on client. 您无法在客户端上执行C#。 That's it. 而已。 Both languages are Turing complete, so don't worry :) 这两种语言都是图灵完整的,所以不要担心:)

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

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