C# 程序员如何使用 JavaScript 编写解决方案?

发布于 2024-09-01 19:02:32 字数 592 浏览 4 评论 0原文

更新:也许我原来的帖子并不清楚这一点,但我主要感兴趣的是在构建解决方案时了解如何构建 javascript 代码的最佳实践,而不仅仅是学习如何使用 API(尽管这确实很重要)。

我需要向网站添加功能,我们的团队决定使用 Web 服务来实现该解决方案,该服务从网站内接收来自 JSON 格式的 AJAX 请求的调用。 Web 服务已创建并且运行良好。现在我的任务是编写解决方案的 javascript/html 部分。

如果我在 C# 中解决这个问题,我将创建单独的类来格式化请求、处理 AJAX 请求/响应、解析响应,最后以某种方式将响应插入到 DOM 中。我会在每个类中适当地构建属性和方法,并尽最大努力在适当的情况下分离功能和结构。

但是,我必须在 javascript 中解决这个问题。首先,我怎样才能像上面描述的从 C# 中处理它一样,在 javascript 中处理我的解决方案?或者更重要的是,在 javascript 中构建代码的更好方法是什么?

任何建议或网络上有用材料的链接将不胜感激。

注意:虽然可能与这个问题没有立即相关,但值得注意的是我们将在我们的解决方案中使用 jQuery。

UPDATE: Perhaps this wasn't clear from my original post, but I'm mainly interested in knowing a best practice for how to structure javascript code while building a solution, not simply learning how to use APIs (though that is certainly important).

I need to add functionality to a web site and our team has decided to approach the solution using a web service that receives a call from a JSON-formatted AJAX request from within the web site. The web service has been created and works great. Now I have been tasked with writing the javascript/html side of the solution.

If I were solving this problem in C#, I would create separate classes for formatting the request, handling the AJAX request/response, parsing the response, and finally inserting the response somehow into the DOM. I would build properties and methods appropriately into each class, doing my best to separate functionality and structure where appropriate.

However, I have to solve this problem in javascript. Firstly, how could I approach my solution in javascript in the way I would approach it from C# as described above? Or more importantly, what's a better way to approach structuring code in javascript?

Any advice or links to helpful material on the web would be greatly appreciated.

NOTE: Though perhaps not immediately relevant to this question, it may be worth noting that we will be using jQuery in our solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月如刀 2024-09-08 19:02:32

如果您使用 jQuery,那么您已经拥有了一组丰富的 Ajax 工具。一旦您开始编写代码,我怀疑您会发现考虑到框架中已有的功能,并没有您想象的那么复杂。 研究 jQuery API!

编辑至于构建代码,好吧,我再次提供建议,考虑以您自己的 jQuery 插件的形式添加功能。这很容易做到,并且可以(如果小心完成)使您的代码可维护和可重用。我花了一段时间才开始这样思考事情。在习惯 jQuery 的过程中,我倾向于考虑将 jQuery 对象作为参数传递给的实用程序:

function myCode(jq) {
  if (jq.is('div')) {
    // ...
  }
}

现在我在旧代码中找到了这些并蠕动,因为它们确实应该作为 jQuery 插件来完成:

jQuery.fn.myCode = function() {
  if (this.is('div')) {
    // ...
  }
  return this;
};

使用这么小的插件比我最初编写的那些笨拙的函数要干净得多。

当然,并非所有事情都可以或应该这样做。这只是我的经验的一些建议。

If you're using jQuery, you've already got a rich set of Ajax tools. Once you start writing the code I suspect you'll find that there's not as much complexity as you think, given the capabilities already in the framework. Study the jQuery APIs!

edit As to structuring code, well, again I'd offer the advice to consider adding functionality in the form of jQuery plugins of your own. It's easy to do, and it can (if done with care) make your code maintainable and reusable. It took me a while to come around to thinking about things that way; while getting used to jQuery, I had a tendency to think in terms of utilities to which I'd pass a jQuery object as a parameter:

function myCode(jq) {
  if (jq.is('div')) {
    // ...
  }
}

Now I find those in my old code and squirm, because they really should be done as jQuery plugins:

jQuery.fn.myCode = function() {
  if (this.is('div')) {
    // ...
  }
  return this;
};

Much cleaner to use such little plugins than clumsier functions as I originally wrote them.

Of course not everything can or should be done that way; that's just some advice from my experience.

唠甜嗑 2024-09-08 19:02:32

我本来打算推荐 JQuery - 它非常适合处理 JSON/AJAX 请求。

听起来您最关心的是封装;你想把你的担忧分开。在创建 OOP 解决方案时,Javascript 与 C# 的感觉不同,但您仍然可以通过 Javascript 利用许多 OOP 功能。这是开始使用 Javascript 的 OOP 功能的好地方:

http://www.javascriptkit.com/ javatutors/oopjs.shtml

最后,您可以创建一个类来一起处理您的每个需求(格式化、执行 AJAX 查询、处理 AJAX 响应):

$.DataHandler = new function()
{
    this.MyData = "Default Value",
    this.FormatData = function() { return this.MyData; }
    this.HandleResponse = function(data) { ... do something ... }
    this.DoAJAX = function()
    {            
        $.ajax({
            type: "GET",
            url: "/path/to/your/ajax",
            data: this.FormatData(),
            dataType: "json",
            success: this.HandleResponse
        });
    }
} 

我还没有测试上面的代码,但是您得到了主意。稍后,你可以做这样的事情:

$.DataHandler.MyData = "Some other data";
$.DataHandler.DoAJAX();

无论如何,这就是基本的想法。您可以使用 Javascript 进行很多 OOP/封装,具体取决于您的风格和要求。

-道格

I was about to recommend JQuery - it's fantastic for working with JSON/AJAX requests.

It sounds like your primary concern is encapsulation; you'd like to separate your concerns. Javascript has a different feel from C# for creating OOP solutions, but you can still take advantage of many OOP features with Javascript. Here's a good place to get started with Javascript's OOP features:

http://www.javascriptkit.com/javatutors/oopjs.shtml

In the end, you can create a class that handles each of your requirements together (formatting, performing AJAX query, handling AJAX response):

$.DataHandler = new function()
{
    this.MyData = "Default Value",
    this.FormatData = function() { return this.MyData; }
    this.HandleResponse = function(data) { ... do something ... }
    this.DoAJAX = function()
    {            
        $.ajax({
            type: "GET",
            url: "/path/to/your/ajax",
            data: this.FormatData(),
            dataType: "json",
            success: this.HandleResponse
        });
    }
} 

I haven't tested the above code, but you get the idea. Later, you can do something like this:

$.DataHandler.MyData = "Some other data";
$.DataHandler.DoAJAX();

Anyhow, that's the basic idea. There's a lot of OOP/encapsulation you can do with Javascript, depending on your style and requirements.

-Doug

失去的东西太少 2024-09-08 19:02:32

实际上,您使用 jQuery 的事实是非常相关的。 jQuery 将为您提供许多原本需要抽象或封装的东西。例如,仅使用 Javascript,您可能希望封装基于浏览器的 Request 对象的创建。 jQuery 会为您处理此问题,并使用 $.ajax() 及其相关的 $.get$.post.根据您想要对返回的信息执行的操作,完全可以接受不要过度构建您的 javascript 并执行以下操作:

$.get('TestService.asmx/HelloWorld', function(data) {
  $('#resultElement').html(data);
});  

请记住,javascript 必须由客户端加载,除非必须,否则不要压低它。您习惯在 C# 中使用的许多面向对象原则并不是真正必要的。

Actually the fact that you are using jQuery is pretty relevant. jQuery will give you a lot of the things you would otherwise abstract away or encapsulate. E.g. just using Javascript, you would probably want to encapsulate the creation of a Request object based on browser. jQuery takes care of this for you, as well as handling the response by using $.ajax() and its relatives $.get and $.post. Depending on what you want to do with the returned information, it is perfectly acceptable to not over architect your javascript and do something like:

$.get('TestService.asmx/HelloWorld', function(data) {
  $('#resultElement').html(data);
});  

Remember that javascript has to be loaded by the client, don't weigh it down unless you have to. A lot of the OO principles you're used to using with C# aren't really necessary.

手心的温暖 2024-09-08 19:02:32

如果您使用 jQuery,它应该非常简单,大部分背景内容(格式化请求、处理响应...等)都已为您完成。

您可能想研究 jquery.getJSON()

特别是回调函数将是您将在其中处理返回的 JSON - 您将在其中解析数据...等

if you are using jQuery it should be pretty simple, most of the background stuff (formatting the request, handling the response...etc) is done for you.

you probably want to look into jquery.getJSON()

specifically the callback function is going to be where you would handle the returned JSON -where you would parse the data...etc

笨笨の傻瓜 2024-09-08 19:02:32

这正是人们使用 jQuery 的目的。它涵盖了 Ajax 和 DOM 操作,因此学习 jQuery 站点和 jQuery 文档 应该可以帮助您一起破解一些东西。

一般来说,JavaScript 很难使用,因为它看起来就像典型的 Algol 家族语言,但行为却很微妙。 (例如,JavaScript没有具有块作用域。)如果您想投入一些时间,可以阅读的基本书籍是Javascript:好的部分。作者在他的网站上有一些有趣的文章。

顺便说一句,你的设计非常好,而且 JS 是面向对象的,所以你当然可以实现它。 JS 只是在继承和封装方面与主流 OO 语言不同(分别是原型和闭包)。上面引用的书中对此进行了足够详细的介绍。

This is just the sort of thing people use jQuery for. It has Ajax and DOM manipulation covered, so the learning jQuery site and the jQuery docs should help you hack something together.

In general, JavaScript is tough to work with because it looks just like a typical Algol-family language, but misbehaves in subtle ways. (For instance, JavaScript does not have block scope.) If you want to invest some time, the basic book to get is Javascript: The Good Parts. The author has some interesting articles at his website.

Your design, btw, is perfectly fine, and JS is object-oriented, so you could certainly implement it. JS just does inheritance and encapsulation differently (prototypes and closures, respectively) than mainstream OO languages. This is all covered in adequate detail in the book cited above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文