gpt4 book ai didi

javascript - 当关联函数中的原型(prototype)值丢失时,函数会中断

转载 作者:行者123 更新时间:2023-12-03 08:04:46 25 4
gpt4 key购买 nike

如何检查唐老鸭在“function inTheForest(object)”中是否有名称,并且如果它不显示警报(“未找到名称”),然后继续显示约翰人的警报?

function Duck() {}

Duck.prototype.quack = function() {alert('Quaaaaaack!');};
Duck.prototype.feathers = function() {alert('The duck has white and gray feathers.');};
//Duck.prototype.name = function() {alert('Donald Duck');};

function Person() {}

Person.prototype.quack = function() {alert('The person imitates a duck.');};
Person.prototype.feathers = function() {alert('The person takes a feather from the ground and shows it.');};
Person.prototype.name = function() {alert('Rick James');};

function inTheForest(object) {
object.quack();
object.feathers();
object.name();
}

function game() {
var donald = new Duck();
var john = new Person();
inTheForest(donald);
inTheForest(john);
}

game();

最佳答案

您可以通过typeof查看

像这样

if(typeof object.name === "undefined"){
console.log("no name found")
}

关于javascript - 当关联函数中的原型(prototype)值丢失时,函数会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34430228/

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