gpt4 book ai didi

javascript - 为什么我的整个对象没有从哺乳动物对象继承属性?

转载 作者:行者123 更新时间:2023-11-29 10:35:17 24 4
gpt4 key购买 nike

我正在试验原型(prototype)继承,当我将其输入控制台时我没有收到错误,但它似乎没有正确继承。

var organism = {
hasCellDivision: true
};

var animals = Object.create(organism);
animals.hasNervousSystem = true;

var mammals = Object.create(animals);
mammals.legs = 4, hasBrain = true, hasBlood = true, heads = 1;

var plants = Object.create(organism);
plants.hasNervousSystem = false;

var whale = Object.create(mammals);
whale.leg = 0;

var insects = Object.create(mammals);
insects.legs = 6, hasBrain = false, hasGanglia = true;

最佳答案

我觉得问题出在这里

var mammals = Object.create(animals);
mammals.legs = 4, hasBrain = true, hasBlood = true, heads = 1;

应该是

var mammals = Object.create(animals);
mammals.legs = 4, mammals.hasBrain = true, mammals.hasBlood = true, mammals.heads = 1;

关于javascript - 为什么我的整个对象没有从哺乳动物对象继承属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com