简体   繁体   English

将函数从 class 传递到 class

[英]Passing functions from class to class

I want to inherit all the methods from source classes into my main target class/function.我想将源类中的所有方法继承到我的主要目标类/函数中。 I kind of did something, but I wonder if there are better or gentler ways to do this.我做了一些事情,但我想知道是否有更好或更温和的方法来做到这一点。
The idea is that I can keep good readability and separate methods in groups (files) so I know what belongs where.这个想法是我可以保持良好的可读性和组(文件)中的单独方法,所以我知道什么属于哪里。
PS Sorry for my bad english PS对不起我的英语不好

Here's how I did it:我是这样做的:

 function Main(){ const self = this self.name = 'Main' self.speak = () => { console.log(`called in class Main by class ${this.name}`) } } class A{ //fake variables for IDE autofill //no constructor needed speakA(){ console.log(`called in class A by class ${this.name}`) } } class B{ speakB(){ console.log(`called in class B by class ${this.name}`) } } class C{ speakC(){ console.log(`called in class C by class ${this.name}`) } };(function assignOFunctionsToObject(target, ...sources){ sources.forEach(source => { Object.getOwnPropertyNames(source.prototype).forEach(name => { if(typeof source.prototype[name] === "function") { target.prototype[name] = source.prototype[name] } }) }) })(Main, A, B, C) let main = new Main() main.speak() main.speakA() main.speakB() main.speakC()

firstly is thinking about OOP, it's not good idea, add function inside existing object as it is in your example.首先是考虑 OOP,这不是一个好主意,在现有的 object 中添加 function ,就像在您的示例中一样。

But if you want to add dynamically behavior to an existing object, you should use one of those patterns:但是,如果您想向现有 object 添加动态行为,您应该使用以下模式之一:

https://refactoring.guru/design-patterns/proxy https://refactoring.guru/design-patterns/proxy

https://refactoring.guru/uk/design-patterns/visitor https://refactoring.guru/uk/design-patterns/visitor

Implementing utility functions within and across different modules one could at least choose 3 different approaches...在不同模块内和跨不同模块实现实用程序功能至少可以选择 3 种不同的方法......

  1. Write a function based mixin which is a single function with ( this aware) methods bound to the function's this context.编写一个基于 function 的 mixin ,它是一个单一的 function,方法绑定this this

    • apply the imported mixin to whichever object is in need of the mixin's methods. apply导入的mixin应用于需要mixin方法的object。
  2. Write an object based mixin which is a single object with ( this aware) methods.编写一个基于 object 的 mixin ,它是一个带有( this意识)方法的单个 object。

    • assign the imported mixin to whichever object is in need of the mixin's methods. assign导入的 mixin 分配给需要 mixin 方法的 object。
  3. Write and export ( this aware) functions .编写和导出( this感知)函数

    • make the imported functions part of an ad-hoc created object and assign the latter to whichever object is in need of the just created mixin's methods.使导入的函数成为临时创建的 object的一部分,并将后者assign需要刚刚创建的 mixin 方法的任何 object。

 // module... function_based_utility_methods_mixin_A.js // function speakA() { console.log(`called as 'speakA' by instance of type '${ this.type }'`); } function speakAA() { console.log(`called as 'speakAA' by instance of type '${ this.type }'`); } /* export default */function withUtilityMethods_A() { this.speakA = speakA; this.speakAA = speakAA; } // module... object_based_utility_methods_mixin_B.js // function speakB() { console.log(`called as 'speakB' by instance of type '${ this.type }'`); } function speakBB() { console.log(`called as 'speakBB' by instance of type '${ this.type }'`); } /* export default */const utilityMethods_B = { speakB, speakBB, } // module... utility_methods_C.js // /* export */function speakC() { console.log(`called as 'speakC' by instance of type '${ this.type }'`); } /* export */function speakCC() { console.log(`called as 'speakCC' by instance of type '${ this.type }'`); } // module... main.js // import withUtilityMethods_A from 'function_based_utility_methods_mixin_A.js'; // import utilityMethods_B from 'object_based_utility_methods_mixin_B.js'; // import { speakC, speakCC } from 'utility_methods_C.js'; function Main () { this.type = 'main'; this.speak = () => { console.log(`called as 'speak' by instance of type '${ this.type }'`); }; } // option 1... // - APPLY a function based mixin to the // `Main` constructor function's prototype. withUtilityMethods_A.call(Main.prototype); // option 2... // - ASSIGN an object based mixin to the // `Main` constructor function's prototype. Object.assign(Main.prototype, utilityMethods_B); // option 3... // - ASSIGN an ad-hoc created object based mixin // to the `Main` constructor function's prototype. Object.assign(Main.prototype, { speakC, speakCC }); const main = new Main(); main.speak(); main.speakA(); main.speakAA(); main.speakB(); main.speakBB(); main.speakC(); main.speakCC(); console.log({ mainPrototype: Main.prototype });
 .as-console-wrapper { min-height: 100%;important: top; 0; }

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

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