0

I am using string and array prototype to add method. I just want to know why we need to use return statement in string in this example. I use return with array in this example but it is only returning first element in the array. Also when I use return it does not given result in console unless I store them in new array. fiddle

Array.prototype.u= function(){

   for(i=0; i<this.length;i++){
      this[i]= this[i].toUpperCase();
   }
}

var ar= ['a','b','c','d','e','f']
ar.u()
console.log(ar)

String.prototype.u= function (){
   return this.toUpperCase();
}

var st= "bingo"
st.u();
console.log(st)

3 Answers 3

2

" I just want to know why we need to use return statement in string in this example. "

In JavaScript, like in other languages, String is immutable, you can't modify its state, you just can return a new string and do assignment if you need.

So, based on my above answer

var st= "bingo"
st = st.u();
console.log(st)
Sign up to request clarification or add additional context in comments.

Comments

2

The key point is here, I suppose, is that arrays (as reference types) and strings (as primitives) are treated differently when you access their properties.

With the former, it's rather straight-forward: when you write something like this...

var arr = [];
arr.someMethod();

... someMethod is called (if found in arr prototype chain) with arr object supplied as its context (this). But here...

var str = '';
str.someMethod();

... this won't be a string itself - but a new String object, which primitive value is the string stored in str. In other words, it's actually executed as...

var str = '';
new String(str).someMethod();

... which is quite easy to check:

String.prototype.foo = function() { 
  console.log(this);
};
str.foo(); // String {length:0} 

And now, I suppose, it's quite clear why you just have to work with returned values in String.prototype methods: the newly created object is discarded immediately after the method is completed.

Comments

1

Arrays are pass-by-reference. So, in the Array.prototype, any operation you perform results in changes being made on the original array.

However, Strings, numbers, booleans, are pass by value. Any changes you make are not reflected in the original variable. That's why, to make your code work, use:

var st= "bingo"
st = st.u(); // assign result of calling `.u` to `st`

in which you are changing the string's copy, and then returning the result, which is being assigned to st (original). And yes, Strings are immutable.

Further example:

function change(arr){
    // arrays are pass-by-reference, so `arr` refers to the original array passed,
    // and any change in `arr` reflects in the passed array
    arr[0] = 1; // mutation
}

var array = [2, 3, 4];
change(array);  
console.log(array); // [1, 3, 4]

function change2(string){
    // strings are pass-by-value, so `string` does not refer to the original string passed, but is just a copy of the value of the original string
    // and any change in `string` does not reflect in the passed string
    string = '345';
}

var str = '999';
change(str);
console.log(str); // '999'

Demonstration

EDIT:

Thanks to raina77ow

  1. Please refer to his answer to understand how JS makes possible to use something like 'foo'.toUpperCase() even though 'foo' is definitely not an object.

  2. 'foo'['toUpperCase']() translates to 'foo'.toUpperCase(). The former allows this:

    var method = 'toUpperCase'; 'foo'[method]();

5 Comments

I'm sorry, but this is rather bad example. Have you written arr = [1,2,3] in the first snippet, the array would still have been left unchanged. And it doesn't explain at all what happens when you use string methods with accessor notation.
@raina77ow I am sorry if this answer doesn't get up to the standards. 1) arr = [1,2,3] would have changed the first element 1 to 1, so where will the change occur ? 2) accessor notation - meaning str.foo or str[foo] ? I am sorry if I misunderstood anything.
1) arr = [1,2,3] instead of arr[0] = 1, within the function; 2) both actually - it's important to understand how JS makes possible to use something like 'foo'.toUpperCase() even though 'foo' is definitely not an object.
@raina77ow Oh, I see. I tried to keep it simple. 1) I suppose the reference is overwritten, so that won't affect the original. Correct me if I am wrong. 2) I linked it to your answer.
... as well as 'foo'['toUpperCase']().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.