简体   繁体   English

javascript方法链比较

[英]javascript method chaining comparison

I was trying to chain ex1 and ex2 functions and display true or false after comparing value provided in the chained methods. 我尝试链接ex1和ex2函数,并在比较链接方法中提供的值后显示true或false。

function test() {
   var ex1 = function(parm) {
      return this;
   }

   var ex2 = function(par) {
      return this;
   }

   var result = function() {
      console.log('compare and display a boolean result '+ex1(parm).ex2(par));
   }
};
var res = test();
res.ex1('Sam').ex2('Eddie').result();

Thanks in advance for your help. 在此先感谢您的帮助。

You can do this: 你可以这样做:

 function Test() { this.ex1 = function(parm) { this.param1 = parm; return this; } this.ex2 = function(par) { this.param2 = par; return this; } this.result = function() { console.log('compare and display a boolean result ' + (this.param1 == this.param2)); } //return this; }; var res = new Test(); res.ex1('Sam').ex2('Sam').result(); 

Edited my answer so as not to pollute the global namespace as suggested by Ced . 编辑我的答案,以免污染Ced所建议的全局名称空间。 Also, if you go this route, move your methods from within constructor function to prototype object of Test . 同样,如果走这条路线,请将您的方法从构造函数内部移至Test原型对象。 So, as an example ex1 should be written like 因此,例如,ex1应该写成

Test.prototype.ex1 = function(parm) {
               this.parm = parm;
               return this;
           }

Same goes for other methods. 其他方法也一样。

You can use a object literal, and make this more readable: 您可以使用对象文字,并使其更具可读性:

 var Test = { that: function(param) { this.param1 = param; return this; }, isEqualTo: function(param) { this.param2 = param; return this.param1 === this.param2; } }; console.log(Test.that('abc').isEqualTo('cba')); // false console.log(Test.that('123').isEqualTo('321')); // false console.log(Test.that('asd').isEqualTo('asd')); // true console.log(Test.that('tre').isEqualTo('tre')); // true // or just reusing Test.that('word'); console.log(Test.isEqualTo('drow')); // false console.log(Test.isEqualTo('world')); // false console.log(Test.isEqualTo('word')); // true 

function test() {
   var ex1 = function(parm) {
      return this;
   }

   var ex2 = function(par) {
      return this;
   }

   var result = function() {
      console.log('compare and display a boolean result '+ex1(parm).ex2(par));
   }
};

So when this executes it returns nothing: 因此,当执行此命令时,它什么也不返回:

var res = Test();

Thus res is undefined. 因此res是不确定的。 Thus ex1 doesn't exists on undefined by definition. 因此,ex1在未定义的情况下不存在。

res.ex1('Sam').ex2('Eddie').result();

What the answer above me suggest is to return this. 我上面的答案建议是返回这个。 Don't do that. 不要那样

function test() {
  this.ex1 = function(parm) {
    this.param1 = parm;
    return this;
  }

  this.ex2 = function(par) {
    this.param2 = par;
    return this;
  }

  this.result = function() {

    console.log('compare and display a boolean result ' + (this.param1 == this.param2));
  }
  return this;
};

When it enters the function this equals windows. 当它进入功能this等于窗口。 Thus you are polluting the window object. 因此,您正在污染窗口对象。

What you could do instead: 您可以做什么:

function Test() {
   this.parm;
   this.par;
   this.ex1 = function(parm) {
      this.parm = parm;
      return this;
   }

   this.ex2 = function(par) {
      this.par = par;

      return this;
   }

   this.result = function() {
      console.log('compare and display a boolean result '+ (this.parm === this.par));
   }
};
var res = new Test();

res.ex1('Sam').ex2('Eddie').result();

You call new Test() : a new object is created {} where the this value points to it. 您调用new Test():在{}处创建一个新对象,该值指向该对象。 Then the function is executed. 然后执行该功能。 So the object now contains the functions and doesn't pollute your window. 因此,该对象现在包含函数,并且不会污染您的窗口。

You could return an object with a function inside and if the inner function is called, the return theresult without using another function for calling. 您可以返回一个内部带有函数的对象,如果调用了内部函数,则返回结果而无需使用另一个函数进行调用。

 function test(value1) { return { equals: function (value2) { return value1 === value2; } }; } console.log(test(1).equals(1)); console.log(test(1).equals(2)); 

For using the above with an explict calling of result , you could add a result property in the return object, like 为了将以上内容与显式调用result ,您可以在return对象中添加result属性,例如

 function test(value1) { return { equals: function (value2) { return { result: function () { return value1 === value2; } }; } }; } console.log(test(1).equals(1).result()); console.log(test(1).equals(2).result()); 

The nearly the same as above with an object with all properties 具有所有属性的对象几乎与上面相同

 function test(value1) { var v2, object = { equals: function (value2) { v2 = value2; return object; }, result: function () { return value1 === v2; } }; return object; } console.log(test(1).equals(1).result()); console.log(test(1).equals(2).result()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM