是否可以赋予javascript偏类行为(如C#)或像Ruby那样进行猴子修补? [英] Is it possible to give javascript partial class behavior like C# or monkey patching like Ruby does?

查看:41
本文介绍了是否可以赋予javascript偏类行为(如C#)或像Ruby那样进行猴子修补?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部分类背后的想法是,您可以将某些功能组合在一起.在C#中最好的例子是将控件定义放在一个文件中,将事件处理程序放在另一个文件中.在Ruby中,您可以使用Monkey修补程序来替换整个函数等,以使代码完成您想要做的事情.

The idea behind partial classes is that you can group certain functions together. The best example of this in C# is putting control definitions in one file and the event handlers in another. In Ruby, you can use Monkey patching to replace entire functions etc to get code to do something you want it to.

我还没有找到这样做的理由,但是随着网络的发展,我认为更多的应用程序将在客户端,因此我想知道我在服务器中是否找到了一些很棒的功能-其他语言,我也可以在Java语言中使用.

I haven't found a reason to do this yet, but I figure as web improves, more of an application is going to be on the client side so I'm wondering if some of the great features I find in server-side languages, I can also use in Javascript.

有人知道吗?

推荐答案

// file 1

function augment() {
    this.monkey = "monkey";
}

// file 2

function augmentMore() {
    this.patch = "patch";
}

// file 3

var o = {};
augment.call(o);
augmentMore.call(o);
console.log(o.monkey + o.patch);

猴子修补工作.局部类可以按惯例工作.例如,考虑一下此约定.

Monkey patching works. partial classes can work by convention. For example consider this convention.

// file main
function SomeObject() {
    for (var i = 0, ii = SomeObject.Partial.length; i < ii; i++) {
         SomeObject.Partial[i].apply(this, arguments);
    }
}

SomeObject.Partial.SomeName = function() {
   ...
}

// file extra
SomeObject.Partial.SomeOtherName = function() {
   ...
}

JavaScript非常强大.您是否在寻找一个特定的例子?

JavaScript is surprisingly powerful. Was there a specific example you were looking for?

这篇关于是否可以赋予javascript偏类行为(如C#)或像Ruby那样进行猴子修补?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆