【问题标题】:How to avoid the `this` and `new` keywords in Javascript? [closed]如何避免 Javascript 中的 `this` 和 `new` 关键字? [关闭]
【发布时间】:2015-07-19 11:11:14
【问题描述】:

这是我的代码:我想避免使用 this(第 3 行)和 new(第 23 行)。我想这样做是因为 Douglas Crockford 认为它们有害。

"use strict";
function capWord(inStr) {
   this.firstIndex = function(inStr) {
   document.write(capWord.upper(inStr[0])); 
   capWord.space(inStr); 
   };

   capWord.space = function(inStr) {
      for(var i=1;i<inStr.length;i=i+1) { 
         document.write(inStr[i]);
         if (inStr[i] == " ") {
            document.write(capWord.upper(inStr[i+1]));
            i = i+1;
          }
       }
   };

   capWord.upper = function(charUpper) {
      return charUpper.toUpperCase();
   }
}

var insCapWord = new capWord();
insCapWord.firstIndex("learning cool stuff");

【问题讨论】:

  • 你为什么要这样做?你试过自己做吗?
  • 为什么要避开thisnew
  • JsLint 可能在抱怨它,但它几乎对所有事情都抱怨......
  • 我从未使用过 JsLint,但我必须说我对它无法解析有效的常用语法感到不满意:代码 for(var i = 0; i &lt; 10; i++) {} 上的“Unexpected 'for'” 什么?
  • 可以重写代码以使其通过 jslint,即使使用 new 关键字(这是通过使用 CapWord 而不是 capWord 来完成的),但这并没有成功非常好的代码。 Douglas Crockford 有一些关于语言的优点,但jslint 工具只强制执行一些特定的规则,它不会对代码质量进行任何分析。例如,它没有捕捉到构造函数将函数附加到自身。

标签: javascript jslint


【解决方案1】:

如果你有一个构造函数,你需要使用new 来创建对象。您可以在函数中创建一个对象并返回它。

(请注意,这是一个“匿名”对象,而不是capWord 的实例。)

将函数作为属性分配给函数本身没有多大意义。如果你只在函数内部使用它们,你可以让它们成为函数内部本地声明的常规函数​​。

"use strict";

function capWord(inStr) {
    return {
        firstIndex: function (inStr) {
            document.write(upper(inStr[0]));
            space(inStr);
        }
    };

    function space(inStr) {
        for (var i = 1; i < inStr.length; i = i + 1) {
            document.write(inStr[i]);
            if (inStr[i] == " ") {
                document.write(upper(inStr[i + 1]));
                i = i + 1;
            }
        }
    }

    function upper(charUpper) {
        return charUpper.toUpperCase();
    }
}

var insCapWord = capWord();
insCapWord.firstIndex("learning cool stuff");

由于您的原始代码是作为一个类实现的一半,我想展示它如何看起来完全作为一个类实现:

"use strict";

function CapWord(inStr) {
    this.str = inStr;
}

CapWord.prototype = {
    firstIndex: function() {
        document.write(this.upper(this.str[0]));
        this.space();
    },
    space: function() {
        for (var i = 1; i < this.str.length; i = i + 1) {
            document.write(this.str[i]);
            if (this.str[i] == " ") {
                document.write(this.upper(this.str[i + 1]));
                i = i + 1;
            }
        }
    },
    upper: function(charUpper) {
        return charUpper.toUpperCase();
    }
};

var insCapWord = new CapWord("learning cool stuff");
insCapWord.firstIndex();

【讨论】:

  • 谢谢@guffa,非常有帮助。
  • 这个函数后面没有加分号是有原因的吗? function upper(charUpper) { return charUpper.toUpperCase(); }
  • @user3421311:是的,因为没有理由放一个。 space 函数之后也不应该有一个,我修复了它。很抱歉造成混乱。
  • 谢谢。你能告诉我为什么 firstIndex 对象后面需要一个分号吗?
  • @user3421311:那是因为它是一个声明:return { ... };
猜你喜欢
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多