JavaScript从子对象调用父函数(JavaScript call parent function from child object)

编程入门 行业动态 更新时间:2024-10-27 09:43:14
JavaScript从子对象调用父函数(JavaScript call parent function from child object)

我是使用javascript进行面向对象编程的新手。 并试图实现我们从其他编程语言知道的基础知识。

我有两个班,一个家长和一个孩子。 父母有一些私人功能和一些公共功能。 孩子将继承父类。

我希望从子对象访问父公共函数。 我在做什么:

function SuperClass { //Constructor implementation of this class //public functions return { parentPublicFunction: function() { //Public function implementation } } function privateFunction() { //Private function implementation. } } //Inheriting childClass.prototype = Object.create(SuperClass.prototype); childClass.prototype.constructor = childClass; childClass.prototype.parent = SuperClass.prototype; function childClass { //Call to super constructor this.parent.constructor.call(this, arguments); //Public function for childClass. return { childPublicFunction: function() { //function implementation } } } var childObj = new childClass(); childObj.parentPublicFunction();

运行此操作时出现此错误:

Uncaught TypeError:undefined不是一个函数

我在做一些不正确的事吗? 提前致谢!

I am new to Object Oriented Programming with javascript. And trying to implement the basics that we know from other programming languages.

I have two classes, one parent and one child. Parent have some private functions and some public functions. Child will inherit parent class.

And I wish to access parent public function from child object. What I am trying to do:

function SuperClass { //Constructor implementation of this class //public functions return { parentPublicFunction: function() { //Public function implementation } } function privateFunction() { //Private function implementation. } } //Inheriting childClass.prototype = Object.create(SuperClass.prototype); childClass.prototype.constructor = childClass; childClass.prototype.parent = SuperClass.prototype; function childClass { //Call to super constructor this.parent.constructor.call(this, arguments); //Public function for childClass. return { childPublicFunction: function() { //function implementation } } } var childObj = new childClass(); childObj.parentPublicFunction();

I am getting this error when running this:

Uncaught TypeError: undefined is not a function

Am I doing something which is incorrect.? Thanks in advance!

最满意答案

因为你从构造函数返回自己的对象,所以你必须做更多的管道工作。

//Call to super constructor this.parent.constructor.call(this, arguments);

返回SuperClass的公共API,但你没有对结果做任何事情。 你必须将它合并到孩子的公共API中:

//Call to super constructor var parentAPI = this.parent.constructor.call(this, arguments); //Public function for childClass. return Object.assign(parentAPI, { childPublicFunction: function() { //function implementation } });

分配给childClass.prototype在你的情况下实际上是不相关的,因为你从构造函数返回的对象不会继承它。 即, childObj不会拥有任何您分配给原型的属性(如parent )。 通过返回自己的对象还有更多的东西,比如能够使用instanceOf ( childObj instanceOf childClass将为false )。

为了完成所有这些工作,您必须将方法分配给构造函数(而不是返回对象)。 您可以在MDN文档中找到关于该类型OOP的更多信息。


注:约定以大写字母即ChildClass开始构造函数的名称。

Because you are returning your own objects from the constructor functions, you have to do a little more plumbing.

//Call to super constructor this.parent.constructor.call(this, arguments);

returns the public API of SuperClass but you are not doing anything with the result. You'd have to merge it into the child's public API:

//Call to super constructor var parentAPI = this.parent.constructor.call(this, arguments); //Public function for childClass. return Object.assign(parentAPI, { childPublicFunction: function() { //function implementation } });

Assigning to childClass.prototype is actually irrelevant in your case, because the object you return from the constructors doesn't inherit from it anyway. I.e. childObj won't have any of the properties you assigned to the prototype (such as parent). There are even more things you miss out by returning your own object, such as being able to use instanceOf (childObj instanceOf childClass will be false).

To make that all of that work you have to assign the methods to this inside the constructor (instead of returning an object). You can find more information about that style of OOP in the MDN documentation.


Note: It is convention to start the names of constructor functions with a capital letter, i.e. ChildClass.

更多推荐

本文发布于:2023-07-30 13:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   对象   从子   JavaScript   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!