Sample Function接受对象作为参数并以两种不同的方式修改对象属性。首先使用。 (点)表示在函数调用之外反映,第二表示创建新哈希。
function myFunc(theObject)
{
theObject.make = "Toyota";
theObject = {make: "Ford", model: "Focus", year: 2006};
console.log(theObject.make); // logs "Ford"
}
var mycar = {make: "Honda", model: "Accord", year: 1998};
console.log(mycar.make); // logs "Honda"
myFunc(mycar); // Call function to change the 'make'
console.log(mycar.make); // logs "Toyota"
答案 0 :(得分:0)
您正在调用myFunc(mycar)函数传递对象的引用。 当你在做什么
theObject.make = "Toyota";
您正在直接更改值(即对象状态),存储对象。
当你再次创建对象相同的引用指向新对象时,该范围仅限于该函数。
Oustide myFunc(mycar):现有引用仍然指向旧对象,其中对象状态由function myFunc(theObject)
简而言之:
theObject.make = "Toyota"; //Here we are changing the state
theObject = {make: "Ford", model: "Focus", year: 2006};// Here we are crating new Object
范围仅限于该功能。
theObject.make =“Toyota”chaing现有对象状态的差异 andObject = {make:'Ford'}正在创建新对象