English 中文(简体)
如何采用像 j等链式方法?
原标题:How to implement chained method calls like jQuery?

So I am (still) completely in love with the almighty jQuery, and I have my own growing library of utilities that I want to codify in a java-script object. And I would like to keep syntax similar to that of jquery for the sake of simplicity for my other front end devs. So I want something like this:

 foo(argument).method(argument);

我一直在努力这样做:

var foo = function(str){
    this.str = str;
}

foo.prototype = {
    alertTest  :  function(additional){
         alert(this.str +     + additional);
    }
}

因此, f( h)al(世界);

我知道这一点是可能的,但我不是一个联络处的幌子,需要帮助获得这一简单的权利。 请提供帮助。 我也打算拥有许多oo().)、bar( like)和 f().)等类型功能。 我曾几次尝试过,但在此艰难挣扎。 还必须有非常严格的办法。

感谢民间!

最佳回答

你们几乎在那里:

new foo( hello ).alertTest( world );

或如您不喜欢<代码>新:

var bar = function bar(str) {
    this.str = str;    
};

bar.prototype = {
    alertTest :  function(additional){
        alert(this.str +     + additional);
        return this;
    }
};

function foo(str) {
    return new bar(str);
}

foo( hello ).alertTest( world );

rel=“nofollow”>Live Demo。

问题回答

此前我曾做过这样的事情,是创造!

如果我正确记住,为了能够使用权宜之计,我必须把物体作为原始功能呼吁的一部分归还。 这样,我就能够像<条码>(id)美元(df)项)和(pff0000 )

function $(id){
    this.e = document.getelementbyid(id)
    me = this
    this.val = function (newval) {
        this.e.value = newval;
        return me;  // <- Important
    };
    return this;  //  <- Important
}

$("textbox1").val("New Value")    // changes textbox1 s value to "New Value"

http://www.mikedoesweb.com/vis/rel=“nofollow” http://www.mikedoesweb.com/vis/

我说得很快,但你可以与我们试图在这里实现的本质有关——

function ChainingObj() {
  if (!(this instanceof ChainingObj)) {
    return new ChainingObj();
  }
}

ChainingObj.prototype.first = function() {
  console.log("foo");
  return this; //important to return this.
}


ChainingObj.prototype.second = function() {
  console.log("bar");
  return this;
}

var a = ChainingObj().first().second();

回答这一问题已经很晚了,而且 j忙也非常pre。 但是,人们经常问这个问题。

在不使用原型的情况下,我执行如下:

const wrapper = (person) => ({
    sayGoodMorning:() => {
        console.log("Good Morning, "+person.name)
        return wrapper(person);
    },
    showAlert:() => {
       alert("Hey I am alert");
       return wrapper(person);
    },
    sayGoodEvening:() => {
        console.log("Good Evening, " + person.name)
        return wrapper(person);
    },
    sayGoodNight:() => {
        console.log("Good Night, " + person.name)
        return wrapper(person);
    },
  });
  const person = {name:"StackOverflow"};
  const $ = (obj) => wrapper(obj);
  const $example = $(person);
  
  $example
    .showAlert()
    .sayGoodMorning()
    .sayGoodEvening()
    .sayGoodNight();




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签