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());
Related
This question already has answers here:
Dynamic function name in javascript?
(24 answers)
Closed 2 years ago.
I need to define a function with a name that I have in the string.
Not a big deal, right?
window[fnName] = function(...args) {
// ...
}
But what if I cannot access the window object? Like inside of Worker, for example.
Is it possible to do it without window and without eval()?
You can use self. It is property in both window and Worker scope that references back to their global scope. MDN/Window/self
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:
Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
(10 answers)
Closed 7 years ago.
I am still getting used to Javascript (I'm coming from C++) and would like to add a new function to String which mutates the string instance directly. Let's say that I want to add a new character at the midpoint of the string (ignoring any error checking). In C++ you could do something like this->value = .... Is that the way to do this in Javascript? TIA
String.prototype.mutateSelf = function(param1) {
// How do I mutate this specific string instance?
return this;
};
Javascript strings are immutable. You must construct a new string and return it.
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();
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);