Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

javascript - 从文件导出类时,Instance的类方法未定义(Class methods undefined for Instance when class is exported from file [closed])

I have a class BinarySearchTree in a file ( a.js ) which I've exported then imported BinarySearchTree in other file and use it as a parent class for one of other class Traversal .(我在导出的文件( a.js )中有一个BinarySearchTree类,然后在其他文件中导入BinarySearchTree并将其用作其他类Traversal的父类。)

The issue, I felt is that the method present in the parent class is not accessible in the child class (throws error as :(我感到的问题是,父类中存在的方法无法在子类中访问(引发错误为:) error TS2339: Property 'getRootNode' does not exist on type 'Traversal'.(错误TS2339:类型“ Traversal”上不存在属性“ getRootNode”。) Note: I tried keeping both the class in the same file and it works without error.(注意:我尝试将两个类都放在同一文件中,并且可以正常工作。) I'm new to Typescript but well versed with ES Standards.(我是Typescript的新手,但精通ES标准。) Please highlight, If i'm missing any details here or if there is a work around.(请突出显示,如果我在这里缺少任何详细信息或有解决方法。) Code Segment: a.ts(代码段: a.ts) export class BinarySearchTree { root: any; constructor() { // root of a binary seach tree this.root = null; } getRootNode() { return this.root; } } b.ts(b.ts) import { BinarySearchTree } from './a.ts'; export class Traversal extends BinarySearchTree{ constructor() { super() } } var _traversal = new Traversal(); _traversal.getRootNode()   ask by SJC translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try this(尝试这个)

constructor() { this.data = 123; this.testMethod = this.testMethod.bind(this); } testMethod() { console.log('test') } Now you have access to testMethod() of this class.(现在,您可以访问此类的testMethod()。) As you can see I just created a property instance named testMethod which represents testMethod() in the current class.(如您所见,我刚刚创建了一个名为testMethod的属性实例,该实例表示当前类中的testMethod() 。) Since this pointer refers to encapsulated class that is why you can access to its members.(由于this指针指向封装的类,因此您可以访问其成员。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...