Passing a string as a parameter by reference

I want to change the contents of a string in a function like

function appendSeparating(s, s2, separator) {
    if (s != "")
        s += separator;
    s += s2;
}

I would like to change s on return, but since the string is a primitive, it is passed by value, so modifications do not affect the original.

What is the most efficient / clean way to handle this? (I try to keep the code concise)

+4
source share
4 answers

Global variables

While string primitives are not passed by reference, one of them is not mentioned, it is the ability to use global variables. From my own conscience, I should advise this, but without knowing your use case, you should know about your options:

s = 'a'                       // created as a global variable
appendSeparating('b', '|')    // function now takes only two arguments
console.log(s)                // global variable affected

function appendSeparating(s2, separator) {
  // s is a global variable
  if (typeof s === 'undefined')
    return;

  if (s != "")
    s += separator;
  s += s2;
}
Run codeHide result

Return destination

, . , , ( ) - , , :

String.prototype.append = function(str, delimiter) {
  return [this, str].filter(v => v !== '').join(delimiter || '|')
};


let ex1 = 'a'
ex1 = ex1.append('b')
console.log('no delim: ', ex1)


let ex2 = 'a'
ex2 = ex2.append('b', '-')
console.log('w/ delim: ', ex2)
Hide result

"" . , , . , - :

let ex1 = 'a'
ex1 = append(ex1, 'b')
console.log('no delim: ', ex1)


let ex2 = 'a'
ex2 = append(ex2, 'b', '-')
console.log('w/ delim: ', ex2)


function append(prefix, suffix, delimiter) {
  return [prefix, suffix].filter(v => v !== '').join(delimiter || '|')
};
Hide result

/

, , . , , , , ( ):

const strings = {}

// Assuming key name
strings.s = 'foo'
append(strings, 'bar', ': ')
console.log(strings.s)

// Supplying key name
strings.x = 'x'
appendNamed(strings, 'x', 'y')
console.log(strings.x)



function append(str, suffix, delimiter) {
  str.s = [str.s, suffix].filter(v => v !== '').join(delimiter || '|')
};

function appendNamed(str, strName, suffix, delimiter){
  str[strName] = [str[strName], suffix].filter(v => v !== '').join(delimiter || '|')
};
Hide result
+1

JavaScript out, , . - .

function appendSeparating(stringObj, s2, separator) {
    if (stringObj.text != "")
        stringObj.text += separator;
    stringObj.text += s2;
}
+7

.

function appendSeparating(s, s2, separator) {
    s += s && separator;
    return s + s2;
}

var x = '';

console.log(x = appendSeparating(x, 'one', ', '));
console.log(x = appendSeparating(x, 'two', ', '));
console.log(x = appendSeparating(x, 'three', ', '));
Hide result

, .

function appendSeparating(object, key, s2, separator) {
    object[key] += object[key] && separator;
    return object[key] += s2;
}

appendSeparating(clients[index].address, 'postalCode', 'foo', ', ');
+1

, .

: , " " , ,

function concatStrings(stringObj, s2, separator) {
    stringObj.value = stringObj.value + separator + s2;
}

var baseString = "hello";
var stringObj = {value: baseString};
var s2 = "world";
var separator = " ";

concatStrings(stringObj, s2, separator);

window.alert(stringObj.value);
+1

Source: https://habr.com/ru/post/1694489/


All Articles