Why first one with obj . retreat() and second object passed as argument

can anyone explain the difference of . … calling function… in the end of both code.
1 - alienShip.retreat(); 2 - greenEnergy(spaceship);
why first one with obj . retreat() and second object passed as argument
thanks

let retreatMessage = ‘We no longer wish to conquer your planet. It is full of dogs, which we do not care for.’;

let alienShip = {

retreat(){

 console.log(retreatMessage);

},

takeOff(){

 console.log('Spim... Borp... Glix... Blastoff!');

}

}; alienShip.retreat();

alienShip.takeOff();

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

let spaceship = {

‘Fuel Type’ : ‘Turbo Fuel’,

homePlanet : ‘Earth’

};

// Write your code below

let greenEnergy = obj =>{

obj[‘Fuel Type’] = ‘avocado oil’;

} ;

let remotelyDisable = obj =>{

obj.disabled = true;

};

greenEnergy(spaceship);

remotelyDisable(spaceship);

alienShip.retreat();

This first goes into the alienShip object. Then it finds the retreat key which executes the function within’s code:

(){

 console.log(retreatMessage);
},

There is no parameter in this function. It needs no argument to run console.log(retreatMessage);


greenEnergy(spaceship);

This takes the spaceship object as an argument and executes the greenEnery function.

let greenEnergy = obj =>{    //this has an obj parameter. When you call greenEnery(spaceship) spaceship is passed into the function in place of obj as an argument.

obj[‘Fuel Type’] = ‘avocado oil’;   //Here it uses the argument spaceship to execute this code (like this: spaceship['Fuel Type'] = 'avocado oil';)

} ;

This changes the spaceship ‘Fuel Type’ to ‘avocado oil’.

1 Like

understand now thank you sir