Skip to content

Instantly share code, notes, and snippets.

@chapuzzo
Last active July 17, 2019 11:31
Show Gist options
  • Save chapuzzo/703771e7b3c46077f9d8a3580e8df00a to your computer and use it in GitHub Desktop.
Save chapuzzo/703771e7b3c46077f9d8a3580e8df00a to your computer and use it in GitHub Desktop.
Paso de valores entre dos módulos de javascript
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
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.' })
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