简体   繁体   English

在其静态方法内访问类

[英]Access a class inside its static methods

I am building a simple class . 我正在建立一个简单的class My question is: 我的问题是:

How to access a class method, or this for that matter, inside a static class method? 如何访问一个类的方法,或者this对于这个问题,一个静态类方法中?

When trying to access this inside a static method like this: 当尝试在thisstatic方法中访问this方法时:

const { ProductsCollection } = require('../utils/Collection')
let modeledCollection = ProductsCollection.mapToModel(legacy)

I get a TypeError : this.generateModel is not a function . 我收到一个TypeErrorthis.generateModel is not a function Here is my class: 这是我的课:

class ProductsCollection {
    generateModel () {
        let model = { name: 'testing' }
        return model
    }

    static mapToModel (legacy) {
        if (!isObject(legacy))
            return legacy

        let current = this.generateModel() // Here!!!
        for (let key in legacy) {
          // some code...
        }
        return current
    }
}

module.exports = { ProductsCollection }

Thanks in advance! 提前致谢!

How to access a class method, or this for that matter, inside a static class method? 如何在静态类方法内部访问类方法?

The only way to access instance information from a static method is to create an instance (or receive one as a parameter, or close over one [which would be very odd], etc.; eg, you need an instance). static方法访问实例信息的唯一方法是创建一个实例(或接收一个实例作为参数,或关闭一个实例(这很奇怪),等等;例如,您需要一个实例)。 This is the point of static methods: They aren't associated with an instance of the class, they're associated with the constructor function. 这就是static方法的重点:它们不与类的实例关联,而是与构造函数关联。

Your generateModel method, as shown, doesn't use the instance either, so it may make sense for it to be static as well. 如图所示,您的generateModel方法也不使用实例,因此它也应该是static Then you'd access it via this.generateModel (assuming mapToModel is called via ProductsCollection.mapToModel ) or ProductsCollection.generateModel (if you don't want to make that assumption): 然后,您可以通过this.generateModel (假设通过ProductsCollection.mapToModel调用mapToModel )或ProductsCollection.generateModel (如果您不希望这样做)来访问它:

 class ProductsCollection { static generateModel() { return {name: "testing"}; } static mapToModel(legacy) { return this.generateModel(); // or `return ProductsCollection.generateModel();` if you want to use // `ProductsCollection` specifically and not be friendly // to subclasses } } console.log(ProductsCollection.mapToModel({})); 

Or if generateModel need instance information, you might use new ProductsCollection (or new this ) in your mapToModel to create an instance, then access generateModel on that instance. 或者,如果generateModel需要实例信息,则可以在mapToModel使用new ProductsCollection (或new this )来创建实例,然后在该实例上访问generateModel

 class ProductsCollection { generateModel() { return {name: "testing"}; } static mapToModel(legacy) { const instance = new this(); // or `const instance = new ProductsCollection();` if you want // to use `ProductsCollection` specifically and not be friendly // to subclasses return instance.generateModel(); } } console.log(ProductsCollection.mapToModel({})); 

You cannot access an instance method as it were a static one. 您无法访问实例方法,因为它是静态方法。 This is a classic error. 这是一个经典的错误。 The only way you can use an instance method inside a static one is if you have an instance of the class available (hence the name "instance method"). 在静态实例中使用实例方法的唯一方法是,如果有可用的类的实例(因此称为“实例方法”)。

Usually you are making so mistake in how you are deciding what should be static and what not...without thinking much I can just suggest making the generateModel method static. 通常你正在这样的错误你是如何决定什么应该是静态的,哪些不是......不假思索多,我可以只建议把generateModel方法是静态的。 Another way would be to remove the generateModel method completely and incorporate it's behaviour inside a constructor. 另一种方法是完全删除generateModel方法,并将其行为合并到构造函数中。 It really depends on what your needs are. 这实际上取决于您的需求。

Just remember that if you are accessing a non static property inside a method then it should not be static. 请记住,如果您要在方法内部访问非静态属性,则该属性应该不是静态的。

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

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