This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
How to turn a String into a JavaScript function call? [duplicate]
(13 answers)
Closed 8 years ago.
How can I execute a Javascript function such this:
cursor.continue(parameters)
By using a string to identify the function name, without using eval? Something like this:
cursor.callMethod("continue", parameters);
Is this possible?
Yes, you can use the square bracket notation.
cursor["continue"](parameters)
cursor["continue"] is exactly the same as cursor.continue.
If you are in control of callMethod, and the function belongs to an object or is global, then yes, that's possible.
For example, if the target function is a method of the same object where callMethod is:
var cursor = {
callMethod: function(method, params) {
this[method].apply(this, params);
},
continue: function() {}
}
cursor.callMethod("continue", [1, 2, 3]);
Yes, you can call the function like so:
cursor["continue"](parameters);
Related
This question already has answers here:
Add method to string class
(6 answers)
Closed 4 years ago.
So, for learning purposes I'd like to recreate the "charAt()" existing method in Javascript which tells you the position of a character in a given string. I'll call the method "CharAtX"
To do so, I've created a function with 2 parameters : The first one is the word, the second is the position, here is the code I have :
function charAtX(word,pos) {
word_split = word.split("");
return(word_split[pos])
}
console.log(charAtX("Truck",2))
So, it obviously works, if i call charAtX("truck",2), i will have "u" returned.
But my question is the following :
The original charAt can be called like such
my_word.charAt(3)
Mine can't though. Why is that and how could I change my function into a method so that I can?
You have to call charAtx function in the context of String object.So, When you call string.charAtx, this object refers to the string. You have to learn prototype and this object in jvascript.
String.prototype.charAtX = function(pos) {
word_split = this.split("");
return(word_split[pos])
}
This question already has answers here:
Add method to string class
(6 answers)
How do I write an extension method in JavaScript?
(2 answers)
Closed 5 years ago.
I need to write a method I will call on a string literal in JavaScript. A method that I want to call:
"Javascript".toKampala();
Does that feature exist in JavaScript? and if it does How do I write such a method (toKampala()) on a JavaScript literal or any object?
In Kotlin I did it like this;
fun String.toHenry():String{
return "$this Henry";
}
and I can call
"chalres".toHenry()
Every string is default has a prototype, which is the String.prototype object and it can access anything which are defined there.
You need add that method in the String.prototype and it will be accessible from any string. You can access the current string in that function by this.
String.prototype.toHenry = function() {
return this + ' Hentry';
};
console.log('charles'.toHenry());
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have a json object like
var json={
key1:val1,
key2:"foo" //as a string
}
and i have foo function
$scope.foo=function{
//something
}
Now on ng-click I want to do something like ng-click="json.key2()"
Basically I want to call this foo function from it's string name ?
Is it possible ?
I know it's bad approach, but is it possible ?
Thanks
You can access properties on an object dynamically by using square-brackets.
For example, in your controller (after declaration of $scope.foo):
$scope.fn = $scope[json.key2];
And in your markup:
ng-click="fn()"
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
I have the following code where MODE is a variable. Its value should be a method of jQuery's tinycolor liberary.
$cor = tinycolor($cor).mode(z).toString();
I'd like to call that method on that line so, for instance, when mode = 'lighten' $cor would be
$cor = tinycolor($cor).lighten(z).toString();
Is there a way of doing it this way?
Thanks!
Use bracket notation
$cor = tinycolor($cor)[mode](z).toString();