Web articles in the first two program ape wechat public number, hope to communicate with you more, common progress ~

Before we talk about the different uses of toString(), let’s talk about the difference between string() and toString() :

1. Both String() and toString() can be converted to strings, but toString() cannot convert null and undefined, because null and undefined have no wrapper of their own and cannot access the object’s toString() method. The property reference of the wrapper object is completed, and the newly created temporary object is destroyed

Which leads to an interview question:

var s = 'test'; s.len = 4; var t = s.len; // What does t outputCopy the code

A string is not an object, so how can it refer to object methods?

Because strings have a wrapper type, you can convert a string into an object that handles references to attributes and destroys the newly created object as soon as the reference ends. Var t = s.len (len); len (len); len (len); len (len); len (len); len (len); len (len)

What about adding an attribute to a reference type?

Var a = [] a.len = 4 console.log(a.len) // 4Copy the code

Because reference types exist in memory until they leave the current scope, objects of wrapper types exist only during code execution and are destroyed immediately afterward. Therefore, we cannot add attributes and methods to primitive data at run time.

ToString () : in the project, the data returned by the server is fully trusted, and the toString() method is called, resulting in the page error, whoooooo ~ friends write code should be careful

ToString ()

  1. Conversion of numbers to base (2-36)
var a = 10;
a.toString(2) // "1010"
a.toString(8) // "12"
a.toString(16) // "a"
Copy the code

Random captcha can be generated using the base conversion of toString()

Math.random().tostring (36).substring(3,7) // generates a random four-digit verification codeCopy the code
  1. Determine the type of data (base data and reference data)
Object.prototype.toString.call(Array) // "[object Function]"
Object.proptotype.toString.call([]) // "[object Array]"
Copy the code

Because if you want to get a real Object of built-in type, we need to get the Object properties of [[Class]], before es5, the attribute value can only pass by the Object. The prototype. ToString to access, therefore, Through the Object. The prototype. ToString. Call (arr) change this points to the toString method, to obtain Object of built-in type.

The [[Class]] internal attribute is the value used internally by the engine to determine which type an object belongs to. All objects (such as groups) whose Typeof return value is "object" contain an internal attribute [[class]]Copy the code

Using the toString() method for ordinary functions returns the contents of the function, and using toString for built-in functions returns a ‘[native code]’ string.

function test(){ alert(1); //test} test.toString(); / *"function test(){ alert(1); //test }"*/ Function.toString(); //"function Function() { [native code] }"
Copy the code

Mention typeof and Instanceof, by the way

  1. Typeof:

Typeof determines data types only correctly for basic data types, but not for reference data types

Two forms of typeof:

Typeof (expression) Typeof variable names // Return values: string, number, Boolean, undefined, object(null also returns object),function, symbol
Copy the code

Such as:

typeof NaN // number

typeof Symbol(1) // "symbol"
Copy the code
  1. A instanceof B:

[object] instanceof [constructor]

Principle: Is instance A in the constructor of B

The “A” on the left must be an object. If it is an underlying type, false will be returned

For complex types of data:

let reg = new RegExp(//)
reg instanceof RegExp // true
reg instanceof Object // true

let date = new Date()
date instanceof Date // true
date instanceof Object // true
Copy the code

So using Object. The prototype. ToString. Call (XXX) returns the data type is the most of, is the return value of the representative of the Object [] Object data type string representation.

Etc. What? Of the Object. The prototype. ToString. Call (XXX) quickly use up.