How to mutate this in a new String function? [duplicate] - javascript

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.

Related

array-like in Javascript detail explanations [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Does javascript have a method to replace part of a string without creating a new string?
(4 answers)
Closed 1 year ago.
var a = 'cat' ;
a[0] = 'r' ;
a = 'cat'
Why..??
In case of string although you can access elements by array notation, if you try to change its content it will fail silently i.e. will not throw any error but will not change content either.
Please explain me detail.
Strings are primitive values in javascript, and are therefore immutable. This is just how the language works so there's not much to explain besides that. You can read more about it here!
It is not throwing an error because you're probably not running it in strict mode.
Strings are immutable, in JavaScript only objects and arrays are mutable. You can search for mutable data types in JavaScript in Google.
"A mutable object is an object whose state can be modified after it is created." MDN.
You can read more here: Mutable

Javascript string update doesn't change the string itself [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
I have following simple javascript code:
s="hello"
s[0]="X"
console.log(s[0])
The output is h, but I have updated it as X, I would ask why I got such result, thanks.
Strings in JavaScript are primitives, which means they are immutable, ie. you are not able to change them. If you were running your code in strict mode then this would throw an error.
If you wanted to modify this string you would need to effectively create a copy of it, and then assign that to the s variable eg.
s = "X" + s.substring(1)

How to write Extensible methods in JavaScript [duplicate]

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());

Given the path of property key, how to retrieve it? [duplicate]

This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Javascript: Get deep value from object by passing path to it as string [duplicate]
(5 answers)
Closed 5 years ago.
Consider I've an object myObj
And I've a string representing the path to some property inside it: foo.bar
What is the best way to get it from my object?
If I knew the string ahead I would do myObj.foo && myObj.foo.bar to get it safely
A simple solution would be to split the string 'foo.bar'.split('.') and than loop over it.
But i'm sure there is a better way
It's a duplicate of other question. they provided a great solution:
given a path and an obj get the property value this way
path.split('.').reduce((o, i) => o[i], obj)
A simple solution would be to split the string 'foo.bar'.split('.') and than loop over it.
Yep, that sounds like the best way. You can create a helper method that does exactly this, but there's nothing built in to the language or standard libraries to make things simpler than this.
function getFromPath(obj, path) {
var current = obj;
for(let piece of path.split('.')) {
current = current[piece];
}
return current;
}
Usage:
getFromPath({foo: {bar: "hello"}}, "foo.bar"); // "hello"

Does defining a frequently used string as a variable improve performance? [duplicate]

This question already has answers here:
Do common JavaScript implementations use string interning?
(3 answers)
Closed 7 years ago.
In some languages, frequently used strings are defined as variables/constants, which are called instead of literal strings. Is this the same with JavaScript? In particular, I have frequent use of the string 'none'. Instead of writing the literal 'none' everywhere in the code, would it improve performance if I define:
var none = 'none';
and use none everywhere in the code? Or, is there a way to intern a literal string expression so that it is evaluated only once?
Literal strings are automatically interned by most Javascript compilers. So var a = 'hello' and var b = 'hello' will likely already be pointing at the same copy of the 'hello' string in memory, no need for further optimization on your part.
The only way to make sure different string objects are created for the same string value is by defining each one via the String global object, i.e.:
var a = new String('hello');
var b = new String('hello');

Categories