没有 child.prototype = new Parent(); 的 Javascript 继承;

发布于 2024-12-24 17:34:07 字数 722 浏览 2 评论 0原文

我正在研究 javascript 原型链继承,并且遇到了这个有趣的行为。

我有一个父类和一个子类

//class parent
function parent(param_1) {
    this.param_1 = param_1;

    this.getObjWithParam = function(val) {
        console.log(this);
        console.log("Constructor value in parent class " + this.param_1);
        console.log("tguha ----> parent,  val " + val);
    };
};

//class child
function child(param_1) {
    parent.call(this, [ param_1 ]);
};

var childObj = new child(100);
childObj.getObjWithParam(200);

,我得到的输出是

**>child**
Constructor value in parent class 100
tguha ----> parent,  val 200

//child.prototype = new Parent(); 并且仍然继承父类。

任何人都可以帮我解释一下这个场景吗?

I was playing around with javascript prototype chain inheritance and i came accross this funny behaviour.

I have a parent class and a child class

//class parent
function parent(param_1) {
    this.param_1 = param_1;

    this.getObjWithParam = function(val) {
        console.log(this);
        console.log("Constructor value in parent class " + this.param_1);
        console.log("tguha ----> parent,  val " + val);
    };
};

//class child
function child(param_1) {
    parent.call(this, [ param_1 ]);
};

var childObj = new child(100);
childObj.getObjWithParam(200);

and i get the output as

**>child**
Constructor value in parent class 100
tguha ----> parent,  val 200

and nowhere i'm doing //child.prototype = new parent();
and still the parent class is inherited.

Could anyone help me by explaining this scenario please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演多会厌 2024-12-31 17:34:07

这段代码中没有出现原型这个词。所以什么都没有被继承。您创建一个新的 child,然后在该新的 child 上显式运行 parent 构造函数。然后,parent 构造函数向新的 child 添加一个方法。

如果您将 getObjWithParam 放在 parent.prototype.getObjWithParam 上,那么您会发现它不会保留。

The word prototype does not appear in this code. So nothing is being inherited. You create a new child and then explicitly run the parent constructor function on that new child. The parent constructor function then add a method to to the new child.

If you put getObjWithParam on parent.prototype.getObjWithParam instead then you will see that it will not carry over.

屌丝范 2024-12-31 17:34:07

您在子构造函数中调用父构造函数。因为父构造函数设置了 this.getObjWithParam = function[...] 它也会为子构造函数设置它。请注意,这与原型无关。
通过调用 parent.call(a, [...]),您可以调用父函数并将作用域设置为 a。这意味着对 this 所做的任何修改也会对 a 进行(因为它是同一个对象)。

You invoke parent constructor in child constructor. Because parent constructor sets this.getObjWithParam = function[...] it'll also set it for child. Notice that this has nothing to do with prototype.
By invoking parent.call(a, [...]) you invoke parent function and set scope to a. That means any modification made to this is also made to a (because it's the same object).

梦初启 2024-12-31 17:34:07
parent.call(this, [ param_1 ]) 

将 Parent.param_1 和 Parent.getObjWithParam 分配给 childObj。这与继承无关。考虑这个公式:

var foo = {};

//class parent
function parent(param_1) {
    this.param_1 = param_1;

    this.getObjWithParam = function(val) {
        console.log(this.param_1);
        console.log(val);
    };
};

//class child
function child(param_1) {
    parent.call(foo, [ param_1 ]);
};

var childObj = new child( 'lets give foo a param_1');

console.log( typeof childObj.getObjWithParam );
// Undefined

foo.getObjWithParam();
// ["lets give foo a param_1"]

这里将 foo 作为范围传递给 .call(),因此属性被分配给 foo。

parent.call(this, [ param_1 ]) 

Assigns Parent.param_1 and Parent.getObjWithParam to childObj. This has nothing to do with inheritance. Consider this formulation:

var foo = {};

//class parent
function parent(param_1) {
    this.param_1 = param_1;

    this.getObjWithParam = function(val) {
        console.log(this.param_1);
        console.log(val);
    };
};

//class child
function child(param_1) {
    parent.call(foo, [ param_1 ]);
};

var childObj = new child( 'lets give foo a param_1');

console.log( typeof childObj.getObjWithParam );
// Undefined

foo.getObjWithParam();
// ["lets give foo a param_1"]

here you are passing foo as a scope to .call() thus the properties are assigned to foo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文