简体   繁体   English

解释,“this”语法 JavaScript

[英]Explanation, “this” syntax JavaScript

So I was making a node app that had a conditional statement in the class constructor, if this then variable is equal to that, else variable equal to that.所以我正在制作一个节点应用程序,它在 class 构造函数中有一个条件语句,如果这个 then 变量等于那个,否则变量等于那个。 However, when I initialize that variable I want to change depending on the boolean statement with "this" syntax it gives me an error in the console.但是,当我初始化该变量时,我想根据带有“this”语法的 boolean 语句进行更改,它在控制台中给我一个错误。 Why does this happen?为什么会这样? And how can I initialize that variable if I can't initialize it more than once with "this" syntax in JS?如果我不能用 JS 中的“this”语法多次初始化它,我该如何初始化该变量?

Update:更新:

I was given How does 'this' work in JavaScript?我被告知“这个”在 JavaScript 中如何工作? to see if this helps, and it has nothing to do with what I asked.看看这是否有帮助,这与我的要求无关。 To be more specific, it's like why can I do this var test; if(true){var test = 1}else{var test = 0}更具体地说,这就像我为什么要进行这个var test; if(true){var test = 1}else{var test = 0} var test; if(true){var test = 1}else{var test = 0} and not this if(true){this.test = 1}else{this.test = 0} and be able to use the variable all throughout my app. var test; if(true){var test = 1}else{var test = 0}而不是 this if(true){this.test = 1}else{this.test = 0}并且能够在我的整个应用程序中使用该变量.

code:代码:

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');
    }

    
  }

console error:控制台错误:

/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)

The first problem is your conditional:第一个问题是你的条件:

this.secret === null || undefined

should be应该

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

if you want to be explicit but you could just use a double equals instead of triple for a truthy answer that would include null or undefined:如果您想明确但可以只使用双等号而不是三等号来获得真实的答案,其中包括 null 或未定义:

this.secret == null

The second problem, which makes it difficult to answer your question, is it's totally unclear what this.words is, what you're trying to get the length of, and why.第二个问题使您难以回答您的问题,完全不清楚this.words是什么,您试图获得的length以及原因。

If you're trying to truncate the last letter of the variable this.words , for example, you would do:例如,如果您尝试截断变量this.words的最后一个字母,您可以:

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

Only strings and arrays have a length , unless you've added a length to your own class's prototype.只有字符串和 arrays 有一个length ,除非你在自己的类的原型中添加了一个 length 。 So this.length wouldn't work unless this is a string or an array or a class with a custom length method.因此this.length将不起作用,除非this是一个字符串或数组或具有自定义长度方法的 class。

I am Assuming that your are calling your class in some other file using a import statement and passing it a random string inside a " " .我假设您正在使用 import 语句在其他文件中调用您的 class 并在" "中传递一个随机字符串。

If that is the case then you need to handle that input inside your class Wallet and sanitize it for the expected input..如果是这种情况,那么您需要在class Wallet中处理该输入并对其进行清理以获取预期输入。

somehow somewhere the given input is having a break code such as \n which is the cause of this error.. maybe a use of typeof() to check that you have a valid string before this line of code不知何故,给定输入的某个地方有一个中断代码,例如\n这是导致此错误的原因.. 可能使用typeof()来检查您在这行代码之前是否有一个有效的字符串

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

can prevent the error..可以防止错误..

and yes the problem in previous answer are there too..是的,之前答案中的问题也存在..

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

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