Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write a custom valueOf method for primitives in Javascript?

Tags:

javascript

Number.prototype.valueOf=function(){
  alert('works!');
}

'str'+123; // no alert
'str'+(new Number(123)); // alert

Is there a way to write custom valueOf() methods for primitives? I.e. is there a way to make 'str'+123 call alert()?

like image 279
Leo Jiang Avatar asked Oct 20 '22 11:10

Leo Jiang


1 Answers

I.e. is there a way to make 'str'+123 call alert()?

No, you have to do something to promote the primitive to its equivalent object first, which that line doesn't do (and I take it you don't want to do). The addition operator (they call it that even when it's concatenation) will use the spec's ToString abstract operation to convert the number to a string. For numbers, the ToString abstract operation does the steps below, which don't involve using anything from the Number or Object prototype.


The ToString abstract operation applied to numbers:

  1. If m is NaN, return the String "NaN".
  2. If m is +0 or −0, return the String "0".
  3. If m is less than zero, return the String concatenation of the String "-" and ToString(−m).
  4. If m is infinity, return the String "Infinity".
  5. Otherwise, let n, k, and s be integers such that k ≥ 1, 10k−1s < 10k, the Number value for s × 10n−k is m, and k is as small as possible. Note that k is the number of digits in the decimal representation of s, that s is not divisible by 10, and that the least significant digit of s is not necessarily uniquely determined by these criteria.
  6. If kn ≤ 21, return the String consisting of the k digits of the decimal representation of s (in order, with no leading zeroes), followed by n−k occurrences of the character ‘0’.
  7. If 0 < n ≤ 21, return the String consisting of the most significant n digits of the decimal representation of s, followed by a decimal point ‘.’, followed by the remaining k−n digits of the decimal representation of s.
  8. If −6 < n ≤ 0, return the String consisting of the character ‘0’, followed by a decimal point ‘.’, followed by −n occurrences of the character ‘0’, followed by the k digits of the decimal representation of s.
  9. Otherwise, if k = 1, return the String consisting of the single digit of s, followed by lowercase character ‘e’, followed by a plus sign ‘+’ or minus sign ‘’ according to whether n−1 is positive or negative, followed by the decimal representation of the integer abs(n−1) (with no leading zeroes).
  10. Return the String consisting of the most significant digit of the decimal representation of s, followed by a decimal point ‘.’, followed by the remaining k−1 digits of the decimal representation of s, followed by the lowercase character ‘e’, followed by a plus sign ‘+’ or minus sign ‘’ according to whether n−1 is positive or negative, followed by the decimal representation of the integer abs(n−1) (with no leading zeroes).
like image 67
T.J. Crowder Avatar answered Oct 22 '22 00:10

T.J. Crowder