简体   繁体   English

方法是否必须在要链接在一起的对象内?

[英]Do methods have to be within an object to be chained together?

I'm trying to understand how chaining functions work in JavaScript. 我正在尝试了解链接功能如何在JavaScript中工作。 I have two examples: 我有两个例子:

First 第一

    class Arithmetic {
      constructor() {
        this.value = 0;
      }
      add(value) {
        this.value = this.value + value;
        return this;
      }
     subtract(value) {
     this.value = this.value - value;
     return this;
     }
   }

You can do chain the methods by instantiating let a = new arithmetic(); 您可以通过实例化let a = new arithmetic(); and a.add(3).subtract(4); a.add(3).subtract(4);

Second 第二

var zappo = function(selector) {
  var el;

  var obj = {
    getEl(selector) {
    return document.querySelector(selector);
    },
    addClass(className){
    el.classList.add(className);
    return this;
    }
  }

  el = getEl(selector);
  return obj;
}

I can chain these methods by zappo(#main).addClass("green").addClass("red"); 我可以通过zappo(#main).addClass("green").addClass("red");链接这些方法zappo(#main).addClass("green").addClass("red");

My question is why is the first constructor function able to chain functions without having the methods within an object whereas the second function requires all the methods to be within an object? 我的问题是,为什么第一个构造函数不能在对象内包含方法的情况下就能链接函数,而第二个函数却要求所有方法都在对象内?

My question is why is the first constructor function able to chain functions without having the methods within an object... 我的问题是为什么第一个构造函数能够在没有对象内包含方法的情况下链接函数...

They methods are in the object, because the object inherits from its prototype, and the methods are defined on the prototype. 他们的方法在对象,因为该对象从它的原型继承,并且所述方法对原型定义。

Do methods have to be within an object to be chained together? 方法是否必须在要链接在一起的对象内?

By definition, methods are only accessible on objects (or JavaScript's primitives that effectively but not literally get promoted to objects), because otherwise we call them functions, not methods. 按照定义,方法只能在对象(或有效但不能从字面上提升为对象的JavaScript的原语)上访问,因为否则我们将其称为函数,而不是方法。

Method chaining isn't anything special. 方法链接没有什么特别的。 All you're doing when you do xa().b() is calling a on x and then calling b on whatever it is a returns. 所有你正在做的,当你做xa().b()被调用ax ,然后调用b上不管它是a回报。 In your class example, each method does return this so each returns the object it was called on. 在您的class示例中,每个方法均return this因此每个方法均返回被调用的对象。 But you can just as easily use xa().b() when a returns an object that isn't x . 但是,当a返回不是 x的对象时,您也可以轻松使用xa().b() It's actually very common. 这实际上很常见。 Example: 例:

document.querySelector("div").addEventListener(/*...*/);

querySelector doesn't return document , it returns the element that was found. querySelector不返回document ,它返回找到的元素。 The above assumes an element will be found and returned (rather than querySelector returning null ) and calls addEventListener on that element. 上面假设将找到并返回一个元素(而不是querySelector返回null ),并在该元素上调用addEventListener

It doesn't require it: 不需要它:

 class zappo {
  constructor(selector) {
    this.el = document.querySelector(selector);
  }

  addClass(className){
   this.el.classList.add(className);
   return this;
 }
}

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

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