gpt4 book ai didi

javascript - 当我们在 Javascript 上创建两个不同的对象/实例时,方法或函数如何做同样的事情?

转载 作者:行者123 更新时间:2023-12-02 20:54:44 24 4
gpt4 key购买 nike

当我读到这篇文章时Prototypes in JavaScript article 在互联网上,我看到了这段代码

function Human(firstName, lastName) {
this.firstName = firstName,
this.lastName = lastName,
this.fullName = function() {
return this.firstName + " " + this.lastName;
}
}

var person1 = new Human("Virat", "Kohli");

console.log(person1);

当他解释这一点时,他提到“两个执行相同操作的函数 fullName 实例。”。我现在很困惑。这个函数如何做同样的事情?因为这个函数将显示两个不同的全名,所以怎么可能做同样的事情呢?任何人都可以帮助我吗?

最佳答案

这意味着 fullName 的输出将始终是新对象的firstName + lastName ...因此 fullName 始终使用新对象的相同属性。

关于javascript - 当我们在 Javascript 上创建两个不同的对象/实例时,方法或函数如何做同样的事情?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61515169/

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