What is the difference between var {todo}= require("./models/todo"); and var todo=require("./models/todo"); [duplicate] - javascript

This question already has answers here:
Curly brackets (braces) in Node.js 'require' statement
(2 answers)
Closed 3 years ago.
I am trying to load a todo model from my todo class and I have two options. The first one is
var {todo}= require("./models/todo");
and second one is
var todo=require("./models/todo");
I am confused which is what.

The first one is a destructuring assignment. It means "take an object from "models/todo" and assign its property "todo" to my local variable "todo". If it contains no such property, you'll get undefined assigned to the variable.

For example this if is your model
module.exports = {
toLower: obj => {
},
streamIdea: async (idea) => {
}
}
if you're doing this
const model = require('mymodel');
then you have to call your functions like this,
model.toLower()
which means you're importing everything and calling it by function name
and if you're importing like this:
const { toLower } = require('mymodel');
it means you're only importing toLower from this model now you can just call it like this
toLower();
without need of model.

Related

Turn a string into function in JS [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 2 years ago.
My problem is simple and I couldn't find the proper answer in this forum. My bad...
I want to do that :
const dataReceived = foo;
foo(state);
How can I do that?
I read it is better to avoid eval, and I couldn't get success with new Function.
Thanks for your help!
EDIT
Thanks for your answers.
I work with React.
In my reducer, I have a create_item case.
I can reach action.category, that can be the word 'currency' or 'country'.
What I want to do is to launch either the method createCurrency or createCountry according what is inside action.category.
That's why I tried to join 'create' and 'action.category' to create a dynamic function name.
But it seems to be a poor idea...
The simplest approach is to create an object which contains an entry where:
the key is a string
the value is a function.
Example:
const myObject = {
myFunction: () => { [... DO SOMETHING...] }
}
Subsequently you will be able to invoke the function, using:
myObject.myFunction();
The above becomes more powerful when you use brackets notation.
Example:
const myString = 'myFunction';
myObject[myString]();

Updating Const variable thorough another file [duplicate]

This question already has answers here:
const or let in React component
(4 answers)
Closed 2 years ago.
I am very new to React and i don't know how to achieve the following case :
I have Variable declared in Config file
Say
Const a= "Some value"
and i want to update that value from different file when certain conditions are met
Assume :
if (true){a= "New Value"}
const is an immutable variable. If you want to have a mutable (changable) variable, use let instead.
You can only initialize a "Const" once!
That's the main difference to all the other ways you can create a variable.

Javascript API syntax help - const { uport, MNID } [duplicate]

This question already has answers here:
What is the difference between const and const {} in JavaScript
(4 answers)
Closed 4 years ago.
So while I was making my react native app, I tried to use an API from
https://github.com/uport-project/react-native-uport-connect and there is a syntax that I've yet to understand.
May I know what does const { uport, MNID } mean from this code
import configureUportConnect from 'react-native-uport-connect'
const { uport, MNID } = configureUportConnect({
appName: 'uPort Demo',
appAddress: '2oeXufHGDpU51bfKBsZDdu7Je9weJ3r7sVG',
privateKey:'<PRIVATE_KEY>',
})
Im quite new to this and this code is placed on a seperate js file and im trying to export const { uport, MNID } so I could use it in my Components and im not sure if it's a variable, object or some js syntax. Thank you!
This is called destructuring, and it means you are assigning your variables, not to the object that the function returns, but to the individual properties of that object, specifically the properties at the keys uport and MNID. The alternative syntax would be to say const variableName = // etc... and then you would access the properties like: variableName.uport.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring

Angular: Change String to variable [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 6 years ago.
Apologies if this is simple. I just couldn't figure-out how to do it. Searched on this site on how to dynamically change variables and evaluate them etc but can't figure out how to do what I want.
Problem: I have a variable that I set to a value:
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
//Here
};
Where it says "Here", I need to instantiate a new variable that is called whatever is the result if the above statement eg; switch1 then set it true or false as a boolean. eg: switch1 = false;
Therefore again, if $switchToggle parameter was "Test", I need to dynamically create a variable called switchTest and set it true or false.
Is such a thing possible ?
Thanks all
Something like that?(I created vm variable just for code snippet, ignore it)
var vm = {};
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
vm[vm.switchValue] = 'whatever';
console.log(vm);
};
vm.toggleDrop('true');
Also if you do not need to attach variable to vm object, best answer will be #nastyklad provided(using window[vm.switchValue]
Something like this:
function setVariable(name){
var variableName = 'switch' + name;
vm[variableName] = true;
}

destructure a parameter and keep reference to it too [duplicate]

This question already has answers here:
ES6 destructuring function parameter - naming root object
(5 answers)
Closed last year.
Is there a way in ES6 to destructure a parameter and reference it by name as well?
myfunction(myparam) {
const {myprop} = myparam;
...
}
Can this be done in a single line in the function parameter list? Something similar to Haskell's # in pattern matching.
There is no syntax support for this. I guess you could hack around this with something like:
const myFunction = (function() {
function myFunction(myparam, {myprop}) {
// ...
}
return function(myparam) {
return myFunction(myparam, myparam);
};
}());
or even
function myFunction(myparam, {myprop}=myparam) {
// ...
}
but both may be considered too hacky.

Categories