Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Object.prototype.toString.apply(value) different from value.toString()?

From what I understand, the former will:

  1. Find the toString method on Object
  2. call it on value but with this bound to value

And value.toString() will.

  1. Find the toString method somewhere in value's prototype chain
  2. Call toString on value bound with this as value via the function invocation pattern

So the difference is if there is an overridden toString method in value... it will use that.

My question is:

  1. Is that the only difference?
  2. Conversely, is this pattern the standard pattern to use if we want to be guaranteed we're calling Parent's method and not potentially some overridden by Child? (In this case Parent = Object, Child = the class value comes from, if we're thinking classically, and method = toString.)
like image 580
djechlin Avatar asked Mar 06 '13 01:03

djechlin


People also ask

What is the return value of the toString () method of an object?

toString() The toString() method returns a string representing the object.

What is the function of toString () method?

For user-defined Function objects, the toString method returns a string containing the source text segment which was used to define the function. JavaScript calls the toString method automatically when a Function is to be represented as a text value, e.g. when a function is concatenated with a string.

What does toString () do in JavaScript?

The toString() method returns a string as a string. The toString() method does not change the original string. The toString() method can be used to convert a string object into a string.

What is the value of a toString ();?

Return Value: The num. toString() method returns a string representing the specified number object.


2 Answers

Object.prototype.toString.apple(value) will let you call on null, while you use null.toString(), it will produce an error.

Object.prototype.toString.apply(null);
>"[object Null]"

null.toString();
>TypeError: Cannot call method 'toString' of null
like image 57
xdazz Avatar answered Nov 09 '22 10:11

xdazz


Object.prototype.toString can be a different method than value.toString() depending upon what the latter is.

> Object.prototype.toString.apply("asdfasdf")
"[object String]"
> "asdfasdf".toString()
"asdfasdf"
> Object.prototype.toString.apply(new Date)
"[object Date]"
> (new Date).toString()
"Tue Mar 05 2013 20:45:57 GMT-0500 (Eastern Standard Time)"

.prototype[function].apply (or .call or .bind) allow you to change the context of a method even though the context may not have such a method at all.

var o = {};
o.prototype = {x: function () { console.log('x'); }}
var y = {}
o.prototype.x.call(y)
y.x(); //error!

...so that is to say that

  1. It's not the only difference
  2. This doesn't necessarily have to do with a relationship between parent and child .. it just allows you to call one object's method (or any function) with a different object context.
like image 41
Explosion Pills Avatar answered Nov 09 '22 10:11

Explosion Pills