使用 Jquery(甚至只是 Javascript),如何将命令链接在一起

发布于 2024-11-10 04:24:56 字数 259 浏览 0 评论 0原文

jquery 中有多个函数,您可以执行以下操作: $('#element').each().get('title').othercmd()

如何创建一个类(或一系列类)来复制此行为? 基本上,我想要这样的东西:

test = new Something()
test.generateSection('title').addData('somedata')

什么是正确的?

谢谢

There are several functions in jquery which you can do the following:
$('#element').each().get('title').othercmd()

How can I create a class ( or a series of classes ) to replicate this behavior?
Basically, I want to something like this:

test = new Something()
test.generateSection('title').addData('somedata')

What is correct for this?

Thanks

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

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

发布评论

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

评论(4

眼泪淡了忧伤 2024-11-17 04:24:56

一种方法是在每个函数中返回“this”(对象)。所以你可以这样做:

<script>
var Something = function() {
  this.hi = function() {
    alert('hi');
    return this;
  };
  this.bye = function() {
    alert('bye');
    return this;
  };
}
var myObj = new Something();
myObj.hi().bye();
</script>

One approach is to return "this" (the object) in each function. So you could do something like this:

<script>
var Something = function() {
  this.hi = function() {
    alert('hi');
    return this;
  };
  this.bye = function() {
    alert('bye');
    return this;
  };
}
var myObj = new Something();
myObj.hi().bye();
</script>
紫﹏色ふ单纯 2024-11-17 04:24:56

只需在每个方法结束时返回您正在操作的内容即可。 (通常是this)。

Just return the thing that you are operating on at the end of each method. (this usually).

﹂绝世的画 2024-11-17 04:24:56

您只需在您希望能够链接的所有方法中返回当前实例即可实现链模式。

Something.prototype.generateSection = function(title){
    ... code ...
    this.sectionAdded = ...;
    return this;
}
Something.prototype.addData = function(data)
{
  ... continue manipulating this.sectionAdded however you need it ..
  return this;
}

并对“类”的其他方法执行相同的操作。需要记住的是,您必须存储将来调用中需要的对象,在您的情况下,您正在生成一个部分,因此您必须将其放入您的实例中(在某些私有变量中,如sectionAdded),这样您就可以能够继续从其他方法操作它。

You can implement a chain pattern just by returning the current instance in all the methods that you want to be able to chain.

Something.prototype.generateSection = function(title){
    ... code ...
    this.sectionAdded = ...;
    return this;
}
Something.prototype.addData = function(data)
{
  ... continue manipulating this.sectionAdded however you need it ..
  return this;
}

And do the same with the other methods of your "class". Something to keep in mind is that you must store the objects that you will need in future calls, in your case you are generating a section, so you would have to put that inside your instance (in some private variable like sectionAdded) so you will be able to continue manipulating it from other methods.

秋日私语 2024-11-17 04:24:56

我不知道是否有一种方法可以轻松地将命令与纯 JavaScript 链接在一起,但如果您想使用 jQuery 尝试此操作,则必须将代码编写为 jQuery 插件。
它实际上非常简单,并且有大量的教程可以帮助您编写自己的插件。

我遇到的最简单的教程之一是本教程:

构建你的第一个jquery-plugin-that.html

I don't know if there is a way to easily chain together commands with plain javascript, but if you wanna try this with jQuery, you'll have to write your code as a jQuery plugin.
It's actually pretty easy and there are tons of tutorials for writing your own plugins.

One of the easiest I came across is this tutorial:

building-your-first-jquery-plugin-that.html

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