JavaScript中的链接方法调用

In JavaScript sometimes we can chain method calls, like this:

在JavaScript中,有时我们可以链接方法调用,如下所示:

car.start().drive()

It’s pretty convenient to do so.

这样做非常方便。

Instead of writing

而不是写

car.start()
car.drive()

we can simplify in a one-liner.

我们可以简化一线。

This is possible if each method returns the object itself. In other words, the implementation must be something like this:

如果每种方法都返回对象本身,则可能发生这种情况。 换句话说,实现必须是这样的:

const car = {
  start: function() {
    console.log('start')
    return this
  },
  drive: function() {
    console.log('drive')
    return this
  }
}

It’s important to note that you can’t use arrow functions, because this in an arrow function used as object method is not bound to the object instance.

需要特别注意的是,您不能使用箭头函数,因为在用作对象方法的箭头函数中, this函数未绑定到对象实例。

I like to use arrow functions all the time, and this is one of the cases where you can’t.

我喜欢一直使用箭头功能,这是您无法使用的情况之一。

Chained method calls are great when you are not returning a set of values from the method, otherwise you obviously need to assign a method call to a variable, and chaining is not possible:

当您不从方法返回一组值时,链式方法调用非常有用,否则您显然需要将方法调用分配给变量,并且链式是不可能的:

const result = car.start()
if (result) {
  car.drive()
}

翻译自: https://flaviocopes.com/javascript-chaining/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JavaScript调用C#的方法,可以使用以下几种方法: 1. 使用Ajax请求:在JavaScript使用Ajax技术向服务器发送请求,然后服务器端使用C#处理请求并返回结果。这种方法适用于在客户端通过异步请求调用服务器端的C#方法。 ```javascript $.ajax({ type: "POST", url: "MyPage.aspx/MyMethod", data: JSON.stringify({ parameter: "Hello" }), contentType: "application/json; charset=utf-8", dataType: "json", success: function(result) { console.log(result.d); // 输出返回结果 }, error: function(xhr, status, error) { console.log(error); // 输出错误信息 } }); ``` 在C#,创建一个可供JavaScript调用的静态方法: ```csharp using System.Web.Services; public class MyPage : System.Web.UI.Page { [WebMethod] public static string MyMethod(string parameter) { // 执行一些逻辑 return "Hello, " + parameter; } } ``` 2. 使用SignalR:SignalR是一个实时应用程序框架,可以在客户端和服务器之间建立双向通信。你可以在服务器端使用C#编写代码,并在客户端使用JavaScript通过SignalR调用服务器端的方法。 首先,安装SignalR NuGet包。然后,在服务器端创建一个Hub类: ```csharp using Microsoft.AspNet.SignalR; public class MyHub : Hub { public string MyMethod(string parameter) { // 执行一些逻辑 return "Hello, " + parameter; } } ``` 在客户端使用JavaScript连接到SignalR Hub,并调用服务器端的方法: ```javascript // 引用SignalR库 <script src="~/Scripts/jquery.signalR-2.4.1.min.js"></script> <script src="/signalr/hubs"></script> // 连接到SignalR Hub var hub = $.connection.myHub; // 定义客户端方法 hub.client.myMethodResponse = function(result) { console.log(result); // 输出返回结果 }; // 开始连接 $.connection.hub.start().done(function() { // 调用服务器端方法 hub.server.myMethod("Hello"); }); ``` 这些是通过JavaScript调用C#方法的两种常见方法。具体使用哪种方法取决于你的应用程序需求和技术栈。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值