一种无需传递`.this`作为参数即可检索上下文的方法 [英] A way to retrieve the context without passing `.this` as parameter

查看:63
本文介绍了一种无需传递`.this`作为参数即可检索上下文的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处是有点背景。



所以我有我的父母班。构造函数接收两个对象:第一个是 class ,其中创建了父对象的实例(使用其他继承父类的类)。
第二个对象只是一些参数



parent.js

  class ParentClass {
构造函数(nativeObject,params){
this.nativeObject = nativeObject;
this.param1 = params.param1;
this.param2 = params.param2;
}
//这里有些逻辑
}
module.exports = {ParentClass};

child.js

  class ChildClass扩展ParentClass {
//这里有些逻辑
}
module.exports = {ChildClass};

我在其他班级方法中使用子班级来创建它。我可以有多个如下所示的文件:



usingclasses.js

  let {ChildClass} = require('path / to / my / child / class'); 

let opts = {
param1:‘idkSomeString’;
param2:42;
}


class MyCoolClass {
createChild(){
return new ChildClass(this,opts);
}
}
module.exports = {MyCoolClass};

anotherclasses.js

  let {ChildClass} = require('path / to / my / child / class'); 

let opts = {
param1:‘thatAstringToo’;
param2:123;
}

class AnotherCoolClass {
AlsoCreatesChild(){
return new ChildClass(this,opts);
}
}
module.exports = {AnotherCoolClass};

我可以创建 ChildClass 的多个实例在一类中使用不同的方法:



thirdexample.js

  let {ChildClass} = require('path / to / my / child / class'); 

let opts1 = {{
param1:‘aStringAgain’;
param2:27;
}

let opts2 = {
param1: ABC;
param2:33;
}

class ClassAgain {
firstMethod(){
return new ChildClass(this,opts1);
}

secondMethod(){
return new ChildClass(this,opts2);
}
}
module.exports = {ClassAgain};

我通过的原因。this因为我正在 ParentClass



中使用它,例如用于记录 console.log (我知道这是在$ {this.nativeObject .constructor.name}中创建的);



我要寻找的是每次我创建 ChildClass .this 的方法>

这背后的原因是,在一个文件(类)中,我最多可以创建10个以上的 ChildClass 实例。



在上一个问题中,我已经得到一个答案,可以帮助我使代码更结构化和更易于维护:



而不是:

  createChild(){
return new ChildClass(this,param1,param2,param3,paramN );
}

我在做:

  createChild(){
返回新的ChildClass(this,paramObj);
}

现在,我可以将所有带有参数的对象存储在文件顶部(甚至是单独的文件),并在需要时轻松查找和更改。

...



现在,我正在尝试找到一种方法每次创建 ChildClass

时,都摆脱 .this

ParentClass 是否还有其他方式可以知道 ChildClass 实例在何处生活?



我试图弄清楚我可以使用 .apply .call 做些什么code>,但看起来这不是我要寻找的机器人



更新:
因为我的零在此方面取得了进展,我什至得到了一条评论,很有可能这是不可能的。现在让我们假设我需要一个仅创建LogClass来创建ChildClass实例的类的名称。



如何在创建 ChildCl时检索类的名称而不每次都传递它屁股

  let opts = {
className:'MyCoolClass'
param1:'idkSomeString';
param2:42;
}


class MyCoolClass {
createChild(){
return new ChildClass(opts);
}
}
module.exports = {MyCoolClass};

问题是我必须设置 className:'MyCoolClass'在每个 opts 版本中一次又一次...重复相同的步骤10-15次

解决方案

您可以使用 Reflect.construct



  class ParentClass {构造函数(nat,x){this.nat = nat this.x = x} log(){console.log('from',this.nat.constructor.name,this.x )}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} (参数){return Reflect.construct(ChildClass,[this,params])}} const ca = new ClassAgain()ca.firstMethod()。log()ca.secondMethod()。log() 


Here is a little background.

So I have my parent class. Constructor receives two objects: the first one is the class where the instance of the parent object was created (using others classes which are inheriting parent class). The second object are just some parameters

parent.js

class ParentClass {
    constructor(nativeObject, params) {
        this.nativeObject = nativeObject;
        this.param1 = params.param1;
        this.param2 = params.param2;
    }
    // some logic here
}
module.exports = { ParentClass };

child.js

class ChildClass extends ParentClass {
    // some logic here
}
module.exports = { ChildClass };

I'm using the child class in the other class methods for creating it. I can have multiple files like below:

usingclasses.js

let { ChildClass } = require('path/to/my/child/class');

let opts = {
    param1: 'idkSomeString';
    param2: 42;
}


class MyCoolClass {
    createChild() {
        return new ChildClass(this, opts);
    }
}
module.exports = { MyCoolClass };

anotherclasses.js

let { ChildClass } = require('path/to/my/child/class');

let opts = {
    param1: 'thatAstringToo';
    param2: 123;
}

class AnotherCoolClass{
    alsoCreatesChild() {
        return new ChildClass(this, opts);
    }
}
module.exports = { AnotherCoolClass};

I can create multiple instances of the ChildClass in a different methods within one class:

thirdexample.js

let { ChildClass } = require('path/to/my/child/class');

let opts1 = {
    param1: 'aStringAgain';
    param2: 27;
}

let opts2 = {
    param1: 'ABC';
    param2: 33;
}

class ClassAgain{
    firstMethod() {
        return new ChildClass(this, opts1);
    }

    secondMethod() {
        return new ChildClass(this, opts2);
    }
}
module.exports = { ClassAgain };

The reason why I'm passing .this because I'm working with it in ParentClass

e.g for logging console.log(`I know this was created in ${this.nativeObject .constructor.name}`);

What I'm looking for is the way to get rid of passing .this every time I'm creating a new instance of the ChildClass

Reason behind this is that in one file (class) I can create up to 10+ instances of the ChildClass.

In the previous question I already got an answer which helped me to make the code more structured and more easy to maintain:

Instead of:

createChild() {
        return new ChildClass(this, param1, param2, param3, paramN);
    }

I'm doing:

createChild() {
        return new ChildClass(this, paramObj);
}

So now I can store all my objects with parameters at the top of the file (of even separate file) and easily find and change it if needed.
...

Now I'm trying to find a way to get rid of .this every time I'm creating ChildClass

Is there any other way how ParentClass can know where does this ChildClass instances "lives"?

I'm trying to figure out can I do something using .apply or .call but seems like this is not a droids I'm looking for

UPDATE: Since I have zero progress on this and I even got a comment that most likely this is not possible let's assume for now that I need a name of the class that is creating instance of the ChildClass only for logging.

How can I retrieve the name of the class without passing it every time when creating a ChildClass?

let opts = {
    className: 'MyCoolClass'
    param1: 'idkSomeString';
    param2: 42;
}


class MyCoolClass {
    createChild() {
        return new ChildClass(opts);
    }
}
module.exports = { MyCoolClass };

The problem is that I will have to set className: 'MyCoolClass' in each version of opts again and again and again... Just repeat the same for 10-15 times

解决方案

You can just instantiate your classes using Reflect.construct.

class ParentClass {
  constructor (nat, x) {
    this.nat = nat
    this.x = x
  }
  log () {
    console.log('from ', this.nat.constructor.name, this.x)
  }
}
class ChildClass extends ParentClass{}

class ClassAgain{
  firstMethod () {
    return this.makeInstance({ a: 1 })
  }

  secondMethod () {
    return this.makeInstance({ b: 2 })
  }

  makeInstance (params) {
    return Reflect.construct(ChildClass, [this, params])
  }
}
const ca = new ClassAgain()
ca.firstMethod().log()
ca.secondMethod().log()

这篇关于一种无需传递`.this`作为参数即可检索上下文的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆