[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Object/toString [Back]  [Original]

Object.prototype.toString() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Object.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

toString() , .

In this article

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

const dog1 = new Dog("Gabby");

Dog.prototype.toString = function dogToString() {
  return `${this.name}`;
};

console.log(dog1.toString());
// Expected output: "Gabby"

obj.toString()

, .

toString(), , , . , toString() , Object. , toString() "[object ]", . :

js
var o = new Object();
o.toString(); //  [object Object]

: JavaScript 1.8.5, toString(), null, [object Null], undefined, [object Undefined], 5- ECMAScript . toString() .

: toString

, toString() . toString() . toString() , , , .

Dog theDog, Dog:

js
function Dog(name, breed, color, sex) {
  this.name = name;
  this.breed = breed;
  this.color = color;
  this.sex = sex;
}

theDog = new Dog("", "", "", "");

toString() , , Object:

js
theDog.toString(); //  [object Object]

dogToString(), toString() . , , , , " = ;".

js
Dog.prototype.toString = function dogToString() {
  var ret =
    " " +
    this.name +
    " - " +
    this.sex +
    ", " +
    this.color +
    " " +
    this.breed;
  return ret;
};

js
Dog.prototype.toString = function dogToString() {
  return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;
};

, theDog , JavaScript dogToString(), :

  - ,  

: toString

toString() . Object.prototype.toString() , Function.prototype.call() Function.prototype.apply(), , , thisArg.

js
var toString = Object.prototype.toString;

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

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

Specification
ECMAScript 2027 LanguageSpecification
# sec-object.prototype.tostring


Web Proxy Viewer  |  New URL  |  Original Page