【问题标题】:Explanation, "this" syntax JavaScript解释,“this”语法 JavaScript
【发布时间】:2021-02-17 01:38:11
【问题描述】:

所以我正在制作一个节点应用程序,它在类构造函数中有一个条件语句,如果这个 then 变量等于那个,否则变量等于那个。但是,当我初始化该变量时,我想根据使用“this”语法的布尔语句进行更改,它在控制台中给我一个错误。为什么会这样?如果我不能在 JS 中使用“this”语法多次初始化该变量,我该如何初始化它?

更新:

给了我How does 'this' work in JavaScript? 看看这是否有帮助,这与我的要求无关。更具体地说,这就像我为什么可以这样做 var test; if(true){var test = 1}else{var test = 0} 而不是 if(true){this.test = 1}else{this.test = 0} 并且能够在整个应用程序中使用该变量。

代码:

class Wallet {
  constructor(secret) {
      //ADDED SECRET PARAMETER
    this.secret = secret;
    
    this.balance = STARTING_BALANCE;

    if(this.secret === null || undefined)
    {
        this.keyPair = ec.genKeyPair();
    
        this.publicKey = this.keyPair.getPublic().encode('hex');
    }
    else
    {
        this.keyPair = ec.keyFromPrivate(this.secret);
        
        this.storeKeys = this.keyPair.toString('hex');
        
        //fs.writeFileSync('../secret.json');
        this.publicKey = this.keyPair.getPublic().encode('hex');
    }

    
  }

控制台错误:

/home/main/public_html/Cypher-Network/node_modules/bn.js/lib/bn.js:622
    var w = this.words[this.length - 1];
                      ^

TypeError: Cannot read property '-1' of null
    at BN.bitLength (/home/main/public_html/Cypher-Network/node_modules/bn.js/lib/bn.js:622:23)
    at Point._hasDoubles (/home/main/public_html/Cypher-Network/node_modules/elliptic/lib/elliptic/curve/base.js:332:48)
    at Point.mul (/home/main/public_html/Cypher-Network/node_modules/elliptic/lib/elliptic/curve/short.js:426:17)
    at KeyPair.getPublic (/home/main/public_html/Cypher-Network/node_modules/elliptic/lib/elliptic/ec/key.js:61:26)
    at new Wallet (/home/main/public_html/Cypher-Network/src/wallet/index.js:26:39)
    at Object.<anonymous> (/home/main/public_html/Cypher-Network/src/blockchain/dataBlock.js:4:16)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)

【问题讨论】:

  • 这能回答你的问题吗? How does 'this' work in JavaScript?
  • 所以为了将来的参考和减少混乱,我写的东西按预期工作,它不是在 JS 中滥用它,对吧 epascarello?
  • 研究 JS 变量 SCOPE,在 JS 中的 this var 有意义之前,您需要了解作用域,以及为什么以及如何使用它。

标签: javascript node.js this


【解决方案1】:

第一个问题是你的条件:

this.secret === null || undefined

应该是

this.secret === null || this.secret === undefined

如果您想要明确但可以只使用双等号而不是三等号来获得包含 null 或 undefined 的真实答案:

this.secret == null

第二个问题让您很难回答您的问题,您完全不清楚this.words 是什么,您要获得length 是什么,以及为什么。

例如,如果您尝试截断变量 this.words 的最后一个字母,您可以:

const w = this.words.splice(0, this.words.length - 1)

只有字符串和数组有length,除非你已经为你自己的类的原型添加了一个长度。所以this.length 不会起作用,除非this 是一个字符串或一个数组或一个具有自定义长度方法的类。

【讨论】:

  • 感谢您提供有用的信息,我会在尝试一些事情后回到这个。
  • 这一切都源于我愚蠢的条件错误。谢谢你回答这个问题,我没有眼睛。
  • 我觉得 StackOverflow 会通过建立一个新站点来消除很多反对意见,该站点明确地只是为了让您的代码再次受到关注,以发现愚蠢的自我造成的错误。
【解决方案2】:

我假设您正在使用 import 语句在其他文件中调用您的类,并在 " " 中传递一个随机字符串。

如果是这种情况,那么您需要在您的 class Wallet 中处理该输入,并将其清理为预期的输入..

不知何故,给定输入的某个地方有一个中断代码,例如\n,这是导致此错误的原因.. 可能使用typeof() 来检查您在这行代码之前是否有一个有效的字符串

this.keyPair = ec.keyFromPrivate(this.secret);

可以防止错误..

是的,上一个答案中的问题也存在..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2018-06-29
    • 2013-11-19
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2011-06-02
    相关资源
    最近更新 更多