简体   繁体   English

Javascript:我如何访问嵌套对象内的函数的类属性

[英]Javascript: how can I access a class property to a function inside nested object

I'm working with javascript classes and there's something I want to do but I don't know how. 我正在使用javascript类,我想做但我不知道如何做。 First of all, this is my class: 首先,这是我的班级:

class Company {
  constructor (options) {
    this.employee = options.employee;
    this.algorithm = "AZ1";
  }
}

Now, employee is an object which has another object nested inside of it and that final object contains a series of functions: 现在,employee是一个对象,它有另一个嵌套在其中的对象,最终对象包含一系列函数:

employee {
  code {
    generate [Function: generate]
    alter [Function: alter]
  }
}

Now, my problem is that I want to access the algorithm from any of these functions? 现在,我的问题是我想从这些函数中的任何一个访问algorithm

Btw; 顺便说一句, I'm trying to do so from an object created with that class: 我正在尝试从使用该类创建的对象执行此操作:

const myCompany = new Company({ employee });

So I want to access the algorithm property from the generate function within myCompany . 所以我想从myCompany的generate函数访问algorithm属性。

You should be able to access the algorithm like this: myCompany.employee.code.generate(myCompany.algorithm) 你应该能够像这样访问算法: myCompany.employee.code.generate(myCompany.algorithm)

EDIT: It wasn't clear from your original question that you're trying to keep the algorithm private to the class. 编辑:从您的原始问题中不清楚您是否试图将算法保持为类私有。 Something like this might work: 像这样的东西可能会起作用:

class Company {
  constructor (options) {
    this.employee = options.employee;
    this.algorithm = "AZ1";
  }

  generateEmployee = () => {
    this.employee.code.generate(this.algorithm);
  }
}

This would give you a myCompany.generateEmployee method that the caller could use without passing in the algorithm. 这将为您提供一个myCompany.generateEmployee方法,调用者可以在不传递算法的情况下使用该方法。

Note that this syntax will only work if you're using a transpiler like Babel. 请注意,只有在使用像Babel这样的转换器时,此语法才有效。 If not, you can use this syntax (so your method has the right this context): 如果没有,您可以使用此语法(因此您的方法具有正确的this上下文):

class Company {
  constructor (options) {
    this.employee = options.employee;
    this.algorithm = "AZ1";
    this.generateEmployee = this.generateEmployee.bind(this);
  }

  generateEmployee() {
    this.employee.code.generate(this.algorithm);
  }
}

object1.field1 = object2 is a one-way relation: object1.field1 = object2是单向关系:

  • object1 can access object2 afterwards, via field1 object1可以通过field1访问object2
  • object2 has absolutely no clue about object1 being around, and thus it can not know about its other fields either. object2完全没有关于object1在周围的线索,因此它也无法知道其他字段。

You can pass it as an argument, like object1.field1.dosomething(object1.field2) , which in the actual case translates to: 您可以将其作为参数传递,例如object1.field1.dosomething(object1.field2) ,在实际情况下转换为:

myCompany.employee.code.generate(myCompany.algorithm)

(And of course assumes that generate() is modified to expect such argument. The freshly added option does not affect the pattern) (当然假设generate()被修改为期望这样的参数。新添加的option不会影响模式)

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

相关问题 如何访问Javascript中的嵌套类对象? - How can I access a nested class object in Javascript? 如何在javascript中访问函数内部的类/对象? - How access class/Object inside function in javascript? JavaScript我可以直接访问object里面的一个属性吗? - JavaScript can I directly access to a property inside a property in the object? 安全访问嵌套 javascript 对象内的属性 - Safely access a property inside a nested javascript object 您如何访问匿名函数内部的javascript类属性是同一类 - How can you access a javascript class property inside of an anonymous function is the same class 如何访问 JavaScript 中 object 的属性? - How can I access a property of an object in JavaScript? 如何在不使用 this 关键字的情况下访问 Javascript 中同一类的方法内的类的构造函数中定义的属性? - How can I access a property defined in a constructor of a class inside a method of the same class in Javascript without using this keyword? 如何在javascript中从函数内部访问对象属性,是否可能? - how to access object property from inside function in javascript, is it possible? 在 javascript 如何动态获取 object 的嵌套属性 - In javascript how can I dynamically get a nested property of an object 如何访问数组中的嵌套对象来验证它? - How can I access a nested object inside an array to validate it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM