gpt4 book ai didi

javascript - 从函数访问javascript对象

转载 作者:行者123 更新时间:2023-11-29 17:16:00 25 4
gpt4 key购买 nike

<分区>

为什么第一个起作用而不是后者?? *这只是一个很小的区别,在后一种情况下,我使用简写来访问 cats 对象属性。我读到如果“属性的名称是一个有效的变量名称——当它没有任何空格或符号并且不以数字字符开头时”,这应该没有任何区别。

    //this works 
var cats = {Spot:true};

function addCat (name) { cats[name] = true; }

addCat("white");

console.log ("white" in cats); //true

console.log (cats.white); //true

    //this doesn't work 
var cats = {Spot:true};

function addCat (name) { cats.name = true; }

addCat("white");

console.log ("white" in cats); //false

console.log (cats.white); //undefined

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