1

This is a very very basic question but for some reason I'm struggling and cannot find a good reference to it.

Say I have this JScript code running under WSH:

function func(myStr) {
    var res = "abc";
    myStr = res;
}

function main() {
    var myStr = new String();

    WScript.Echo(myStr);
    func(myStr);
    WScript.Echo(myStr);
}

main();

I passing String object to func and expect func to set the value of object. However, func uses operator = which does copy the content but generate a new reference

I reviewed this post, How do I correctly clone a JavaScript object?, and couldn't find what I was looking for.

Do we have a simple, straightforward solution to this?

Community
  • 1
  • 1
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • JavaScript has quite an obscure idea of object pointers and so, call by reference programming is quite complex – Technohacker Sep 28 '16 at 13:35
  • I've updated the code to emphasize that `myStr` is not global and cannot be treated that way in `func` – idanshmu Sep 28 '16 at 13:35
  • @K3v1n that's so weird that you would have to work like that. I'm a C++ developer and this use case is as common as it can get. If `myStr` was of type object or array that wouldn't have been a problem. – idanshmu Sep 28 '16 at 13:39
  • the above is my opinion as a Java/JS developer. I agree because JavaScript is prototypal rather than pure-oop – Technohacker Sep 28 '16 at 13:43

2 Answers2

1

You can do this 2 ways

Returning the value from func, like

  var myStr = new String();
  myStr = func();

Where func need to be updated to return the value (return res)

Or you could use object but you would need to change some things

function func(myStr) {
    var res = "abc";
    myStr.value = res;
}

function main() {
    var myStr = {};

    WScript.Echo(myStr.value);
    func(myStr);
    WScript.Echo(myStr.value);
}

main();

This way your myStr is a reference to that object. I would advise you to read on JS Objects.

Some references:
http://eloquentjavascript.net/06_object.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

taguenizy
  • 2,140
  • 1
  • 8
  • 26
1

The main thing is: objects are passed by reference. Other things, like strings, arrays, numbers,... are passed by value.

Every time you pass a string to a function, only a copy of its value gets passed to that function (So don't consider your string object to be an object, don't ask about cloning.). But any link to the original variable is lost.

Objects, on the other hand, when you pass them to a function, the address (pointer / reference) of the object is passed to the function, so it's the same variable inside as outside. So anything that happens to that object inside the function will affect the object on the outside (as well).

Now, look at a similar code, but with passing objects

var myObject = {};
myObject.txt = 'Foo';
console.log(myObject.txt);
func(myObject);
console.log(myObject.txt);

function func(obj) {
  obj.txt = 'Hello World';
}
Emmanuel Delay
  • 3,619
  • 1
  • 11
  • 17