Reputation: 1335
Lets say we have a function that has an object inside it and more :
function myFunc() {
var obj = {
"one" : "1",
"two" : "2"
};
// And more code is down here but our main focus is in the obj object.
};
Now considering another function specially designed for the myFunc() which can either add or remove properties from the object obj in myFunc(). For example by considering the function manip() is that special function that can minuplate the obj object inside the function myFunc() :
manip("add", {
"three" : "3"
};
/* Now the obj object inside myFunc() becomes :
var obj = {
"one" : "1",
"two" : "2",
"three" : "3"
}
Again :
manip("remove", "one three");
/* Now the obj object inside myFunc() becomes :
var obj = {
"two" : "2"
}
So, can this be possible ? Can, the manip() function be made ?
Upvotes: 1
Views: 54
Reputation: 560
You could also define your manip function inside myFunc to perform the manipulation.
function myFunc() {
var obj = {
"one" : "1",
"two" : "2"
};
function manip(type, ...options) {
switch(type) {
case 'add':
// perform some validation before assign
Object.assign(obj, ...options)
console.log({type, obj});
break;
case 'remove':
// perform some validation before assign
delete obj[options[0]]
console.log({type, obj});
break;
default:
console.log('No default actions...');
}
}
manip('add', { "three" : "3" });
manip('remove', 'two');
};
myFunc();
Upvotes: 1
Reputation: 287
If your "manip" function is inside the scope of myFunc, it can "see" your object.
function myFunc() {
var obj = {
"one" : "1",
"two" : "2"
};
function manip(prop,value) {
obj[prop] = value;
}
manip("three","3");
console.log(obj);
}
myFunc();
// { one: '1', two: '2', three: '3' }
If your manip function is outside the scope of myFunc, it cannot see what you declared with "var" - the scope of obj is limited to myFunc.
Upvotes: 1
Reputation: 20875
The key here is to
1) Expose your obj
as a public property on your function
2) Instantiate a myFunc
object
3) Pass that object to your manip
function so that it can be modified
function myFunc() {
this.obj = {
one: '1',
two: '2'
};
};
function manip(item, action, parameters) {
if (action === 'add') {
return item.obj = { ...item.obj, ...parameters };
} else if (action === 'remove') {
delete item.obj[parameters];
}
}
// Instantiate a new myFunc Object
const myFuncInstance = new myFunc();
console.log(myFuncInstance.obj);
manip(
myFuncInstance,
'add',
{ three: '3' }
);
console.log(myFuncInstance.obj);
manip(
myFuncInstance,
'remove',
'two'
);
console.log(myFuncInstance.obj);
Upvotes: 1
Reputation: 2734
The question is not very clear but from what I understand, short answer is no.
Please read about scope in JS
The object is inside the scope of the myFunc(), so it can not be accessed from anywhere other than that function.
There 3 ways you can approach this.
1) Globally define the object
this is pretty obvious, take the object outside the function
2) Pass in a pointer to the object to manip()
so in this case manip would take in a 3rd argument
manip(command, value, object)
and perform the given action on the given object
3) Pass in the function (or call it)
I am not too confident on this because the question is very unclear what the purpose of doing so is but but you can pass in a function that will return an object to manip()
manip(command: String, value: String, f: (..) => Object)
myFunc(){
...
return obj
}
manip would call this f, and get the object out of it. This would allow you to generalize manip for many objects, where you can customize the surrounding to define the set of operations that can be performed on this object. This way you encapsulate the mutation and prevent silly side effects
Upvotes: 1