gpt4 book ai didi

JavaScript 范围/this

转载 作者:行者123 更新时间:2023-11-28 11:08:19 26 4
gpt4 key购买 nike

为什么当我这样做时:

function Dog(){
this.firstName = 'scrappy';
}

Dog.firstName 未定义?

但是我可以这样做:

Dog.firstName = 'scrappy';

现在 Dog.firstName 返回“scrappy”?

最佳答案

How come when I do ... Dog.firstName is undefined?

因为...

However then I can do ... And now Dog.firstName returns 'scrappy'?

函数只是对象,因此您可以为其分配任何属性。

关于JavaScript 范围/this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444305/

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