1

I was following an online lesson for beginners, which used the apply/bind methods to set the 'this' context for a function.

I saw that you can chain the bind method directly to the function block, which was new to me. So it got me thinking why cant I chain other methods, other than bind/call/apply, to affect the returned value.

let obj = {
  name: 'john',
};

let sayHello = function() {
  return 'hello there ' + this.name;
}.apply(obj).toUpperCase();

let sayBonjour = function() {
  return 'Bonjour!';
}.toUpperCase();

console.log(sayHello);
console.log(sayBonjour());

In the example above why can I use the .toUpperCase() method on the sayHello function which uses the apply method, and not on the sayBonjour function which does not. In trying to do so I get the error:

'Uncaught TypeError: (intermediate value).toUpperCase is not a function'.

I realize this is not the way string method (or other methods) is/are intended to be used, for learning purposes, I was hoping someone could explain whats preventing me from using the method in such a way.

Many thanks for your time and help

2
  • 2
    The reason why you can use bind, call and apply but not toUpperCase is that there is no such method in Function.prototype (that is, there is no Function.prototype.toUpperCase()). Have a look at the list here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. As toUpperCase() is a method present in String.prototype the IIFE in the answer below will work, since the function itself returns a string. Commented Sep 4, 2018 at 12:03
  • @GerardoFurtado The key word i was missing was 'call'. I was mistaken regarding the function being called or not, and so I was not applying the method to a returned string but to the function itself. Suddenly I understand the purpose of the call method perfectly now. Thankyou very much for your help! Commented Sep 4, 2018 at 15:00

2 Answers 2

1

You can, but you tried to use .toUpperCase on function. You can use it on string which will be returned by function expression. You can use IIFE to achieve this.

let obj = {
  name: 'john',
};

let sayHello = function() {
  return 'hello there ' + this.name;
}.apply(obj).toUpperCase();

let sayBonjour = (function() {
  return 'Bonjour!';
})().toUpperCase();

console.log(sayHello);
console.log(sayBonjour);

Sign up to request clarification or add additional context in comments.

1 Comment

I understand perfectly now regarding the use of an IIFE for this purpose. Thank you very much for your help. As a recap, I thought i was infact using the .toUpperCase() method on the returned string itself, but was mistaken. I have understood better the purpose of the call method which does just that, calls the function which then returned the string I was expecting.
1

This example shows what is happening when your code is exacuted.

function print(value) {
	const str = Object.prototype.toString.apply(value);
	console.log("Type: " + str.slice(7, str.length - 1) + "\tValue: " + value);
}

let obj = {
	name: "john"
};
/*
let sayHello = function() {
	return 'hello there ' + this.name;
}.apply(obj).toUpperCase();
*/
// equals to
{
	console.log("sayHello case");
	let step1 = function () {
		return "hello there " + this.name;
	};
	print(step1);
	let step2 = step1.apply(obj);
	print(step2);
	let sayHello = step2.toUpperCase();
	print(sayHello);
}
/*
let sayBonjour = function() {
	return 'Bonjour!';
}.toUpperCase();
*/
// equals to
{
	console.log("sayBonjour case");
	let step1 = function () {
		return "Bonjour!";
	};
	print(step1);
	let sayBonjour = step1.toUpperCase();
	print(sayBonjour);
}

1 Comment

This answer was immensely helpful, Thank you very much for help! -- I felt the other answer provided was more succinct in its format, although I wish I could select this as the correct answer as well, as the breakdown of what is happening really helped me understand the concept. Thank you again.

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.