The difference between String.prototype.toString() and Object.prototype.toString() in JavaScript! ! !

tags: JavaScript  String  js  front end  jquery  

Object.prototype.toString()

Let's first look at a small example:

function Dog(name) {
  this.name = name;
}

const dog1 = new Dog('boy');

Dog.prototype.toString = function dogToString() {
  return '' + this.name;
}

console.log(dog1.toString());
// Output: "boy"

Every object has a toString() method, which is automatically called when the object is represented as a text value, or when an object is referenced in an expected string manner. By default, the toString() method is inherited by every Object object. If this method is not covered in the custom object, toString() returns "[object type]", where type is the type of the object.

use

  • Use toString() to detect object type

You can get the type of each object through toString(). In order for each object to be detected by Object.prototype.toString(), it needs to be called in the form of Function.prototype.call() or Function.prototype.apply(), passing the object to be checked as the first parameter, Called thisArg.

var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]

//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]

String.prototype.toString()

The following example outputs the string value of a String object:

var x = new String("Hello world");

alert(x.toString())      // output "Hello world"

the difference:

The String object overrides the toString method of the Object object; it does not inherit Object.toString(). For String objects, the toString method returns the string form of the object, which is the same as the return value of the String.prototype.valueOf() method.

Intelligent Recommendation

The difference between != and !== in Javascript

Development tools and key technologies: javascript OF: Chen Jinfeng Grade: Level 18 (5) Date of writing: January 18, 2019 1.!= will be converted to the same type for comparison, !== In addition to com...

The difference between for in and for of in JavaScript

For in traversal is the current index, not suitable for traversing array objects, returning array subscripts. (suitable for traversing objects) For of traversal is the current 'key' value, suitable fo...

The difference between in and hasOwnProperty in javascript

Whenever code reads an attribute of an object, it performs a search with the target having the attribute with the given name. The search begins with the object instance itself. If an attribute with th...

The difference between ECMAscript and Javascript

** The difference between ECMAscript and Javascript ** ECMAScript is a scripting programming language standardized by ECMA-262 by Ecma International (formerly the European Computer Manufacturers Assoc...

What is the difference between for of and for in in javascript?

Let me start by saying: for...in Loop outkey, for...of Loop outvalue 。 for...of Is a new feature introduced by ES6 that fixes the introduction of ES5for...in Insufficient. for...of Can't loop ordinary...

More Recommendation

The difference between =, ==, === in JavaScript

Judgment rule:An equal sign is an assignment operation, == first convert the type and then compare, === first determine the type, if not the same type is directly false. 1. An equal sign (=) 2. Double...

The difference between jquery and javascript

jQuery language features 1, get the document elements quickly jQuery's selection mechanism is built into Css' selector, which provides the ability to quickly query elements in DOM documents and greatl...

The difference between == and === in HTML JavaScript

First, about == Usage: used for general comparison, does not distinguish data types, only distinguishes whether the values ​​are equal; If the data types of the two data are different, the data type w...

The difference between == and === in JavaScript

One, == Used for general comparison, does not distinguish between data types, only distinguishes whether the values ​​are equal; if the data types of the two data are different, the data type will be ...

The difference between for-in and for-of in javascript

For...in is used to read key name traversal, for...of for reading key value traversal for-in grammar: Features: 1.for-in does not consider the non-enumerable properties of the constructed object, just...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top