Created
July 17, 2019 11:03
-
-
Save chapuzzo/0ba6603a7c60a4146cc0ba578daf7ec7 to your computer and use it in GitHub Desktop.
Paso de valores entre dos módulos de javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { config, editName, getConfig, getName, updateConfig, editSetting, getSetting } from './options' | |
// con objetos | |
console.log(getConfig()) | |
config.angle = 45 | |
console.log(getConfig()) | |
// con funciones que manipulan objetos | |
console.log(getConfig()) | |
updateConfig({ | |
speed: 90 | |
}) | |
console.log(getConfig()) | |
// con funciones que manipulan sólo algunos valores | |
console.log(getSetting('angle')) | |
editSetting('angle', 120) | |
console.log(getSetting('angle')) | |
console.log(getConfig()) | |
// con variables | |
console.log(getName()) | |
editName('ricardo') | |
console.log(getName()) | |
function greet (name, status = 'mr.') { | |
console.log(`Hola ${status} ${name}`) | |
} | |
// paso de parámetros con objetos así el orden es indiferente | |
function greetDestructured ({ name, status = 'mr.' }) { | |
console.log(`Hola ${status} ${name}`) | |
} | |
const me = 'ricardo' | |
greet(me, 'dr.') | |
greet(me) | |
greetDestructured({ status: 'sr.', name: 'antonio' }) | |
greetDestructured({ name: 'carlos', status: 'sr.' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = { | |
move: true, | |
speed: 50 | |
} | |
function updateConfig (newConfig) { | |
Object.assign(config, newConfig) | |
} | |
function getConfig () { | |
return config | |
} | |
let name = 'luis' | |
function getName () { | |
return name | |
} | |
function editName (newName) { | |
name = newName | |
} | |
function editSetting (setting, value) { | |
config[setting] = value | |
} | |
function getSetting (setting) { | |
return config[setting] | |
} | |
export { | |
config, | |
updateConfig, | |
getConfig, | |
name, | |
getName, | |
editName, | |
editSetting, | |
getSetting | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment