This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 years ago.
var myObject = {
sub: {
myFunction: function() {
console.log('check');
}
}
}
var callFn = 'sub.myFunction'; // I want it to solve it here, whats going wrong?
myObject[callFn](); // Works, but not with 'sub.myFunction'
myObject.sub.myFunction(); // This works ofc.
I need a generic solution. Can someone explain why the sub.myFunction does not work? Does anyone have a workaround to solve this?
The . character is a valid part of a property name in javascript as long as you enclose it in quotes.
What that means to you:
myObject["sub.myFunction"] - looks for a property named sub.myFunction on myObject. It does not look for a property named myFunction on the sub property of myObject.
myObject.sub.myFunction - does not have the . in quotes so it's treated as a property accessor. That means we look for a myFunction property on the sub property of myObject.
Related
This question already has answers here:
Calling function inside object using bracket notation
(2 answers)
Closed 6 years ago.
I rather have a seemingly trivial issue, but am not able to figure out an efficient approach.
I have a list of about 50 functions to be called such as :
globalClient.funcA(...)
globalClient.funcB(...)
globalClient.funcC(...)
My code should ideally dynamically create the name of the function (funcA / funcB/ funcC and then proceed to actually call that function. My approach below does not work (please note that these aren't exactly the actual names of the functions. I'm only giving these arbitrary names for simplicity of understanding):
var functionName = 'func'.concat('A');
globalClient.functionName
The second line is where it errors out. Now JS thinks that functionName itself is the name of the function. What I want it to do is resolve functionName to funcA and then call globalClient.funcA(...) instead.
I've thought about implementing a switch / case for this but I'm sure there is a far simpler appraoch. Any ideas?
You could use the bracket notation as property accessor.
globalClient[functionName]()
You can use the [ ] operator for accessing the properties.
var globalClient = {
funcA: function(){
console.log('funcA is called');
}
}
var functionName = 'func'.concat('A');
globalClient[functionName]();
This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
I want to pass string name through argument in function and concat in $scope, this is possible?
function test(txt){
$scope.person. + txt; // I want to do $scope.person.status; for example
}
Thanks.
Yes, just do:
function test(txt){
$scope.person[txt];
}
Check Property accessors.
As any object you can access their properties using Dot notation or Bracket notation. When you don't know before hand what property you would be accessing you would use Bracket notation ideally.
function test(txt){
$scope.person[txt] = ''; // We need to assign something to it if not it wont work;
}
Demo example : https://jsbin.com/gihizo/3/edit?html,js,console,output
This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 6 years ago.
I'm not sure if there is a specific name for this way to pull object properties into their own variable? If there is a name, does anyone know?
var object = {
something: 'a string',
another: 'another string',
}
var { something, another } = object;
This is called object destructuring.
Object destructuring, read up on it here:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
A lot of this new syntax isn't fully implemented in all js engines, so be careful when using it!
If you want to learn more about it, checkout this tutorial:
https://www.eventbrite.com/engineering/learning-es6-destructuring/
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 6 years ago.
I have defined an object with properties that have hyphen in their names.
var data = {
"foo-bar": "value",
"this-that": "another value"
}
Now I need to reference this property in JS, but both these ways result in syntax error.
console.log( data.foo-bar )
and
console.log( data."foo-bar" )
So my question is. How can I access a property that contains hyphen in the name in JS?
Disclaimer: The server-side functionality require hyphen-naming of the properties and I don't really feel like rewriting somebody else's whole script that takes the input params like this. And yes, I know this current way is not the most clean approach possible.
You Could use data["foo-bar"] Instead.
This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 8 years ago.
I have a question about referencing objects in javascript.
Say I have a variable that is some object (lets say json) and it is called objOne - (var objOne = someJSONObject;).
If I go ahead and declare
var objTwo = objOne;
will I have two references to the same Object? Kind of like a c pointer?
To sum it up :
assignements are done by value
you never manipulate objects, only object references
This means that
you'll have two references to the same object (you can check that by changing a property of the object)
when you're passed in a variable the value of a primitive, changing your variable doesn't change other variables
EDIT : as it's a duplicate I'll delete this answer in a minute to allow a proper closing if there's no other answer. Please vote to close.
Yes, objects are passed by reference:
function changeVal(obj){
obj.value = "bar"
}
(function checkRefs(){
var myObject = {
value: "foo"
};
alert(myObject.value);
changeVal(myObject);
alert(myObject.value);
})();