JavaScript changes the method of the original object valueOf

  • 2020-05-17 04:46:35
  • OfStack

The example in this article shows how JavaScript changes the original object valueOf. Share with you for your reference. The specific analysis is as follows:

The objects in JS all contain valueOf methods, and we can replace the valueOf of the original object by customizing the valueOf function


function foo() {
  this.valueOf = function() {
   return 'this is my value';
  }
}
var bar = new foo();
Print( bar ); // prints: this is my value
Print( bar == 'this is my value' ) // prints: true
Print( bar === 'this is my value' ) // prints: false

I hope this article is helpful to you in javascript programming.


Related articles: