Keep frozen object as global state [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am playing around with immer.js. Immer.js lock obejct after giving new instance. Is it ok to use this locked object as global state?
windows.initialState = {a: 'a'};
const nextState = produce(initialState , draftState => {
draftState.a = 'b',
});
windows.initialState = nextState;

Yes, you can assign and keep the frozen object to the global state. As long as your global object(initial state) is not declared as const. So, Nothing wrongs with this code.

Related

How can I make a variable static in Js? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want the newid to be incremented when I call the function.
You make the function close over the variable, by declaring the variable in the surrounding context:
let newid = 3;
const generateTemplate = contact => {
// ...
newid++;
};

How to create object inside object in JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to create an object inside an object in JavaScript like this:
{
"test": {
{},
{}
}
}
In more detail, I want to create a new object if "test" doesn't exist in the object, and create a new object in the same location as the old one if "test" exists.
Please help if this is possible.
You mean, like:
theObject["test"] = {};
this?
It's just an assignment, mate!
You need to give a key in every object you create in JavaScript.
E.g.:
var obj = {
objInside: {}
}
you can read more about object literals.

Rule of Naming Variables for Boolean [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
how should I name for variables on type Boolean like 'Mom is away from keyboard'.
var momIsAwayFromKeyboard = mom.isAwayFromKeyborad;
is correct??
You could use a destructuring assignment:
let {isAwayFromKeyboard} = mom;
In this way you need only to write the variable name once and there will no difference b/w key and variable name.
let mom = {
isAwayFromKeyborad:true
}
let {isAwayFromKeyborad} = mom;
console.log(isAwayFromKeyborad)
Usually, you start with 'is'.
I think a good, short, descriptive name could be:
isMomAFK
or
isMomAway
Think of it as a true/false question: 'Is Mom away from keyboard?'

Remove an array-Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like this:
var animals=["cat","dog","snake","rhino"];
But sometimes I have to delete this array(remove it from the dom).
I have tried animals.remove; and $(animals).remove() and animals.remove() but none of them did the trick.Any ideas?
var animals=["cat","dog","snake","rhino"];
then to clear it do:
animals=[];
or
animals.length=0;
or
while(animals.length > 0) {
animals.pop();
}
Just assign the animals array to a value undefined and the array data will be dereferenced and garbage collected.
Donot try to call delete operator that is explicit removal.
animals = undefined
OR
animals = void 0
Thanks
Just Clear The Array
Using This 2 Methods
animals.length =0
animals=[];

Javascript declaration [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi this is a simple question. I was wondering is there any different when you declare something like this. Thanks
selectedData[key](val)
and
selectedData[key] = val
This line selectedData[key](val) is not a declaration, it's calling the function that is stored under the key key in the object selectedData and it's passing the parameter val to that function.
The other line selectedData[key] = val is assigning the value val to the key key in the object selectedData.
In the first case, you're calling whatever is in selectedData[key] as a function with val as an argument while in the second one you're assigning it.

Categories

Resources