i have a class and this class i need to use the static method and in that method i use the varibale but it show me this errror :
Property 'ec' does not exist on type 'typeof Wallet'.ts
and i not need to set static that variable .
how can i sovle this problem ?
export class Wallet {
ec: any;
constructor() {
this.EC = elliptic.ec;
this.ec = new this.EC('secp256k1');
}
static verifySignuture(address: string, data: any, signature: any): boolean {
const keyfromPublic = this.ec.keyFromPublic(address, 'hex');
return keyfromPublic.verify(Utility.GenerateHash(data), signature);
}
}
how can solve this prblem ?
Javascript class != an instance of this class.
Imagine class as some shape defined by a fields inside it. You instantiate this shape by using a new
keyword. In javascript (typescript as well) you can assign your class' instance to a variable. For example
const myWallet = new Wallet();
and then you can have another instance assigned to yet another variable
const someOtherWallet = new Wallet();
In this case myWallet
and someOtherWallet
has the same shape
, but there are two separated instances of the same shape (class). By using static
word, you are assigning field of your shape to shape itself instead of constructed instance. So if your method is static, instead of calling this.myMethod()
you need to call MyClass.myMethod()
- in your case Wallet.verifySignature()
. The difference is about the this
word. It represents a javascript context within you are operating in specific part of the code. You can find more about it here . Advantage of a non-static
methods of a class is being able to access this
context within those methods. If you do not need to access this
you can go ahead and use static method. In first case (when I assigned new instance of a class Wallet
to a local variable named myVallet
, this context
(inside a Wallet
class) is equivalent to a variable named myVallet
, but the difference is from where you are gonna access it. In your case you are trying to access this
context inside a verifySignature
method, which is wrong, because inside static method of a class, this
context is not an instance of your class. Instead remove a static
keyword before method declaration and try something like this:
const wallet = new Wallet();
const signatureVerified = wallet.verifySignature(address, data, signature);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.