javascript - super.call(this) 的正确用法是什么
问题描述
我有一个类函数(不是我最初开发的)......
function Project()
{
Project.super.call(this);
// this._some_var = null;
/* other initial vars */
...
return DefensiveObject.create(this); // see comment below
}
function initialize()
{
//*******Initialize Project************
}
...
return Project;
此函数是运行节点 main.js 所包含的名为“Project.js”的模块的一部分。
return DefensiveObject.create(this); // not return Object.create(this)
DefensiveObject 是一个类,用于防止对象获取或设置未在该类中明确设置的属性。
main.js 调用驻留在我的 Project 类中的 Project.initialize() 。
我的问题是为什么需要调用“Project.super.call(this);”?
解决方案
在 Javascript 中,保留字 super 用于 ES6 类来引用子类的父类,使用它来引用函数是没有意义的。
请阅读这篇解释 super 用法的文章
推荐阅读
- react-native - 是否可以将 React Navigation 与 snap.expo.io 一起使用?
- kubernetes - Kubectl pods are not created
- laravel - laravel 客户端 api 与 guzzle
- mysql - 排序规则将 utf8mb4_unicode_ci 更改为 utf8mb4_general_ci
- reactjs - 当上下文消费者中的任何状态发生变化时,React 是否会重新渲染所有组件?
- python - 如何解决“TypeError:不支持的格式字符串传递给 OSError.__format__”?
- python - 用另一个列表中的格式化文本替换列表中的文本
- python - Parser 必须是字符串或字符流,而不是 Series
- mongodb - 我如何将数据插入到 mongodb 的嵌入式集合中
- amazon-web-services - 有人可以为我揭开 AWS 服务中的 EMR/EC2/Lambda/SNS/SQ 的神秘面纱吗?