使用扩展函数

发布于 2024-12-27 16:41:21 字数 458 浏览 1 评论 0原文

这是我的第一篇文章!我有一个关于 javascript 继承的快速问题。

当然,'extend2' 方法用于使用 for-in 循环从父对象继承子对象。

var extend2 = function (child, parent) {
    var c = child.prototype;
    var p = parent.prototype;
    for (var i in p) {
        c[i] = p[i];
    }
}

我目前正在阅读 Stoyan Stefanov 的《面向对象的 Javascript》。这是一本很棒的书。

谁能给我一个很好的详细解释,说明孩子的原型对象如何没有完全被覆盖或替换,而只是被增强?

当对象继承时,它们是如何复制(原始数据类型)而不是使用extend2函数将其作为引用查找的?

这真的很有帮助,谢谢!

this is my very first post! I have a quick question in regarding inheritance in javascript.

Of course the 'extend2' method is used to inherit child objects from the parent object using a for-in loop.

var extend2 = function (child, parent) {
    var c = child.prototype;
    var p = parent.prototype;
    for (var i in p) {
        c[i] = p[i];
    }
}

I'm currently reading "Object Oriented Javascript" by Stoyan Stefanov. It's an awesome book.

Can anyone give me a good detailed explanation of how the child's prototype object is not entirely overwritten or replaced but it's just augmented?

How is it that when the objects inherit, they copy (primitive data types) instead of being looked up as a reference by using the extend2 function?

This would really help thanks!

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

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

发布评论

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

评论(1

半仙 2025-01-03 16:41:21

JavaScript 中的原始数据类型是通过值而不是引用传递的。因此,当您复制一个值时,它实际上是在复制它,而不是引用它。
传统上,这是因为原语以这种方式在内存中按字面编码(因此原语 int 7 将在内存中编码为 0x7。然而,在处理对象时,它们被编码为指向实际所在内存位置的指针。因此,当您复制该值时,它只是引用指针的副本,而不是该指针所引用的对象。

至于子对象的原型没有被替换,那是因为java中的原型只是。所以原型可能是另一个对象。看起来像:

{
    someField: 5
}

这表明对象的实例将使用名为 someField 的字段进行初始化,其值为 5,使用上面的代码,对象中的每个条目都会被复制到。子原型,但没有删除任何内容因此,如果子原型如下所示:

{
    someField: 10
    someOtherField: 3
}

那么执行上面的extend2命令将覆盖someField,但不会覆盖someOtherField,因此生成的原型将是:

{
    someField: 5
    someOtherField: 3
}

Primitive data types in javascript are passed via value, rather than reference. Thus when you copy a value it is actually copying it, not referring to it.
Traditionally this is because a primitive was literally encoded in the memory in such a way (so the primitive int 7 would be encoded in memory as 0x7. When dealing with objects, however, they are encoded as a pointer to the memory location where the actualy object is. Thus, when you copy the value it is merely a copy of the reference pointer, not the object that that pointer referrs to.

As for the fact that the child's prototype is not replaced, that is because a prototype in java is merely another object. So a prototype may look like:

{
    someField: 5
}

Which would indicate that instances of the object would initialize with a field called someField whose value is 5. with the above code, each entry in the object is copied to the child prototype, but nothing is deleted. Thus if the child prototype looks like:

{
    someField: 10
    someOtherField: 3
}

Then performing the above extend2 command will overwrite someField, but not someOtherField, so the resulting prototype would be:

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