有没有办法像JQuery中的链接方法那样高效地表达Javascript代码?

有没有办法像JQuery中的链接方法那样高效地表达Javascript代码?,javascript,jquery,Javascript,Jquery,在jQuery中,我们可以在一条语句中运行多个方法,以便高效地表达jQuery代码: $("#p1").css("color", "red").html("Hello world!").attr("class","democlass"); Javascript呢 document.getElementById("p1").style.color = "red"; document.getElementById("p1").innerHTML = "Hello world!"; document

在jQuery中,我们可以在一条语句中运行多个方法,以便高效地表达jQuery代码:

$("#p1").css("color", "red").html("Hello world!").attr("class","democlass");
Javascript呢

document.getElementById("p1").style.color = "red";
document.getElementById("p1").innerHTML = "Hello world!";
document.getElementById("p1").setAttribute("class","democlass");

请注意,jQuery中的方法链接对性能没有任何影响。这只是语法上的甜点,可以使代码更短、更漂亮

也就是说,通过将
p1
元素存储在变量中,并使用
classList
对象添加/删除类,您可以对JS代码进行细微的改进,如下所示:

var p1 = document.getElementById("p1");
p1.style.color = "red";
p1.innerHTML = "Hello world!";
p1.classList.add('democlass');

请注意,jQuery中的方法链接对性能没有任何影响。这只是语法上的甜点,可以使代码更短、更漂亮

也就是说,通过将
p1
元素存储在变量中,并使用
classList
对象添加/删除类,您可以对JS代码进行细微的改进,如下所示:

var p1 = document.getElementById("p1");
p1.style.color = "red";
p1.innerHTML = "Hello world!";
p1.classList.add('democlass');

我们永远不应该忘记的性能增强是在可能的情况下只在DOM中查找一次元素:

var p1 = document.getElementById("p1");

p1.style.color = "red";
p1.innerHTML = "Hello world!";
p1.setAttribute("class","democlass");

我们永远不应该忘记的性能增强是在可能的情况下只在DOM中查找一次元素:

var p1 = document.getElementById("p1");

p1.style.color = "red";
p1.innerHTML = "Hello world!";
p1.setAttribute("class","democlass");

性能提高的原因很简单,因为您只需搜索元素一次,然后将其存储在变量中。jQuery通过反复将该变量返回给您来简化这一过程,但是无论是否使用jQuery,您都可以使用显式变量来实现相同的效果

没有jQuery:

var p1 = document.getElementById("p1");
p1.style.color = "red";
p1.innerHTML = "Hello world!";
p1.setAttribute("class","democlass");
使用jQuery:

var p1 = $("#p1");
p1.css("color", "red");
p1.html("Hello world!");
p1.attr("class","democlass");

性能提高的原因很简单,因为您只需搜索元素一次,然后将其存储在变量中。jQuery通过反复将该变量返回给您来简化这一过程,但是无论是否使用jQuery,您都可以使用显式变量来实现相同的效果

没有jQuery:

var p1 = document.getElementById("p1");
p1.style.color = "red";
p1.innerHTML = "Hello world!";
p1.setAttribute("class","democlass");
使用jQuery:

var p1 = $("#p1");
p1.css("color", "red");
p1.html("Hello world!");
p1.attr("class","democlass");

如果您想编写类似于链接的jQuery,诀窍是限制DOM的接触次数并控制何时应用更改。像这样的东西可以做到:

/**
 * Global variable for closed scope functions
 *
 * @param {HTMLElement} node
 * @returns myElementTreaterCls
 */
var notjQuery = (function() {
  var myElementTreaterCls = (function() {
    /**
     * Creates an instance of myElementTreaterCls.
     *
     * @param {HTMLElement} node
     *
     * @memberOf myElementTreaterCls
     */
    function myElementTreaterCls(node) {
      this.node = node;
      /**
       * Styling changes
       *
       * @type {string}
       * @memberOf myElementTreaterCls
       */
      this.currentCss = null;
    }
    /**
     * Applies all changes to the DOM element
     *
     * @returns myElementTreaterCls
     *
     * @memberOf myElementTreaterCls
     */
    myElementTreaterCls.prototype.render = function() {
      if (this.currentCss != null) {
        this.node.style.cssText = this.currentCss;
        this.currentCss = null;
      }
      return this;
    };
    /**
     * Add styling rules to the DOM element
     * If "render" is falsy the rules aren't applied, saving a paint
     *
     * @param {any} [rules={}]
     * @param {boolean} [render=true]
     * @returns myElementTreaterCls
     *
     * @memberOf myElementTreaterCls
     */
    myElementTreaterCls.prototype.addCss = function(rules, render) {
      if (rules === void 0) {
        rules = {};
      }
      if (render === void 0) {
        render = true;
      }
      if (this.currentCss === null) {
        this.currentCss = this.node.style.cssText.toString();
      }
      for (var cssRule in rules) {
        if (rules.hasOwnProperty(cssRule)) {
          var rule = rules[cssRule];
          this.currentCss += " " + cssRule + ": " + rule + ";";
        }
      }
      if (render === true) {
        this.render();
      }
      return this;
    };
    return myElementTreaterCls;
  }());
  //Returns Instantiate function to "notjQuery"
  return function myElementTreater(node) {
    return new myElementTreaterCls(node);
  };
})();
下面是一个使用示例:

//notjQuery代码的简化版本:
var notjQuery=function(){var t=function(){function t(t){this.node=t,this.currentCss=null}返回t.prototype.render=function(){return null!=this.currentCss&(this.node.style.cssText=this.currentCss,this.currentCss=null),this},t.prototype.addCss=function(t,r){void 0==t&&(t,r={}),void 0==r&(r==r&(r=!0),null==this.currentCss&&(this.currentCss=this.node.style.cssText.toString());for(t中的var n)if(t.hasOwnProperty(n)){var s=t[n];this.currentCss+=“+n+”:“+s+”;“}返回r==!0&&this.render(),this},t}();返回函数(r){返回新的t(r)}();

//>>TEST如果您想编写类似于链接的jQuery,诀窍是限制接触DOM的次数并控制何时应用更改

/**
 * Global variable for closed scope functions
 *
 * @param {HTMLElement} node
 * @returns myElementTreaterCls
 */
var notjQuery = (function() {
  var myElementTreaterCls = (function() {
    /**
     * Creates an instance of myElementTreaterCls.
     *
     * @param {HTMLElement} node
     *
     * @memberOf myElementTreaterCls
     */
    function myElementTreaterCls(node) {
      this.node = node;
      /**
       * Styling changes
       *
       * @type {string}
       * @memberOf myElementTreaterCls
       */
      this.currentCss = null;
    }
    /**
     * Applies all changes to the DOM element
     *
     * @returns myElementTreaterCls
     *
     * @memberOf myElementTreaterCls
     */
    myElementTreaterCls.prototype.render = function() {
      if (this.currentCss != null) {
        this.node.style.cssText = this.currentCss;
        this.currentCss = null;
      }
      return this;
    };
    /**
     * Add styling rules to the DOM element
     * If "render" is falsy the rules aren't applied, saving a paint
     *
     * @param {any} [rules={}]
     * @param {boolean} [render=true]
     * @returns myElementTreaterCls
     *
     * @memberOf myElementTreaterCls
     */
    myElementTreaterCls.prototype.addCss = function(rules, render) {
      if (rules === void 0) {
        rules = {};
      }
      if (render === void 0) {
        render = true;
      }
      if (this.currentCss === null) {
        this.currentCss = this.node.style.cssText.toString();
      }
      for (var cssRule in rules) {
        if (rules.hasOwnProperty(cssRule)) {
          var rule = rules[cssRule];
          this.currentCss += " " + cssRule + ": " + rule + ";";
        }
      }
      if (render === true) {
        this.render();
      }
      return this;
    };
    return myElementTreaterCls;
  }());
  //Returns Instantiate function to "notjQuery"
  return function myElementTreater(node) {
    return new myElementTreaterCls(node);
  };
})();
下面是一个使用示例:

//notjQuery代码的简化版本:
var notjQuery=function(){var t=function(){function t(t){this.node=t,this.currentCss=null}返回t.prototype.render=function(){return null!=this.currentCss&(this.node.style.cssText=this.currentCss,this.currentCss=null),this},t.prototype.addCss=function(t,r){void 0==t&&(t,r={}),void 0==r&(r==r&(r=!0),null==this.currentCss&&(this.currentCss=this.node.style.cssText.toString());for(t中的var n)if(t.hasOwnProperty(n)){var s=t[n];this.currentCss+=“+n+”:“+s+”;“}返回r==!0&&this.render(),this},t}();返回函数(r){返回新的t(r)}();

//>>TESTNote jQuery中的方法链接对性能没有任何影响。它只是语法上的糖分,可以使代码更短、更漂亮。@Rorymcrossan:虽然我大体上同意,但这样的链接方法允许不重复搜索,不是吗?完全公开:我对javascript一无所知。:)@SergioTulentsev你是对的。我在下面添加了一个答案,答案是一样的:)用javascript编写的任何东西都比它的jQuery等价物有更好的性能。阅读本文:特别是“最小化重绘和回流”第节介绍如何通过避免重绘和回流来优化DOM操作。请注意,jQuery中的方法链接对性能没有任何影响。让代码变得更短、更漂亮只是语法上的甜点。@RoryMcCrossan:虽然我大体上同意,但这样的链接方法允许不重复搜索,不是吗?完全披露:我对javascript一无所知。:)@塞吉奥图兰采夫,你是对的。我在下面添加了一个同样的答案:)用javascript编写的任何东西都比它的jQuery等价物具有更好的性能。阅读本文:特别是“最小化重绘和回流”部分描述了如何通过避免重绘和回流来优化DOM操作。