Typeof and 0bject. prototype. toString for determining the types and methods of various data in js _ basic knowledge

Source: Internet
Author: User
Remind everyone that Object. prototype. toString (). in the [objectclass] returned by call (param), the first letter of the class is in upper case, such as JSON, or even in upper case. Therefore, you can convert the class to lower case when making a decision to avoid errors. 1. typeof (param) returns the param type (string)

This method is a global method defined in JS, and is also the most commonly used method by compilers. The advantage is that it is easy to use and easy to remember, the disadvantage is that the object, null, array, regexp, and custom object cannot be well judged.

Sample Code:

The Code is as follows:


Var str = 'str ';
Var arr = ['1', '2'];
Var num = 1;
Var bool = true;
Var obj = {name: 'test '};
Var nullObj = null;
Var undefinedObj = undefined;
Var reg =/reg /;

Function fn (){
Alert ('this is a function ');
}

Function User (name ){
This. name = name;
}
Var user = new User ('user ');

Console. log (typeof (str ));
Console. log (typeof (arr ));
Console. log (typeof (num ));
Console. log (typeof (bool ));
Console. log (typeof (obj ));
Console. log (typeof (nullObj ));
Console. log (typeof (undefinedObj ));
Console. log (typeof (reg ));
Console. log (typeof (fn ));
Console. log (typeof (user ));


Result:

The Code is as follows:


String
Object
Number
Boolean
Object
Object
Undefined
Object
Function
Object


2. Object. prototype. toString (). call (param) returns the param type (string, in the format of [object class]).

This method supports most types of judgment. This method is used to determine the type of jquery encapsulation. Some may seem a little confused. Let me break it down for you.

1) call (param) Function

A. fun (). call (B) means in js, let object B Replace a, then execute the fun function of a, and write an example:

The Code is as follows:


Function Class1 ()
{
This. name = "class1 ";

This. showNam = function ()
{
Alert (this. name );
}
}

Function Class2 ()
{
This. name = "class2 ";
}

Var c1 = new Class1 ();
Var c2 = new Class2 ();

C1.showNam. call (c2 );


The output result is class2 rather than class1, which is equivalent to method inheritance.
Therefore, Object. prototype. toString (). call (param) actually means param. prototype. toString (), why don't we write param directly. prototype. toString (), but uses call () to wrap it around. See 2 for details.

2) Object. prototype. toString ()

What is Object ?, Script56.chm (Official M $ tutorial): Obect provides common functions for all JScript objects. In fact, objects are the ancestor of all js objects and are a concept, all objects in js are Object instances, and different objects rewrite their own independent methods. Prototype does not require too much investigation. It returns a prototype reference. However, you can dynamically add methods and attributes to the prototype.
A small example

The Code is as follows:


Function class (){
This. name = "class ";
This. showName = function (){
Alert (this. name );
}
}
Var obj = new class ();
Obj. showName ();
Class. prototype. showNameContact = function (){
Alert ("prototype test" + this. name );
}
Obj. showNameContact ();


The class and prototype test class will be output separately. The showNameContact function is not defined in the original constructor class (). With prototype, We can dynamically add functions to the object prototype, in the example of "new", there will naturally be. Therefore, Object. prototype. toString () means to execute the toString method in the ancestor of the Object.

So what is toString? Many js manuals define the toString () function as follows:
The toString () method converts a logical value to a string and returns the result. Syntax: booleanObject. toString (). As I said just now, all objects in js are inherited objects. All these objects have custom functions or some functions that reconstruct objects, and they all have toString () the function is rewritten. Therefore, we cannot directly write param. prototype. toString () in 1, so that we can execute the toString () function after param overwrites itself.

Well, what is toString () at the crucial moment? What is the role of toString?

In ES3, the Object. prototype. toString method specifications are as follows:

Object. prototype. toString ()

When the toString method is called, the following steps are performed:

1. Obtain the value of the [[Class] attribute of this object.

2. Calculate the three strings "[object", Result (1) of the first step, and new string after.

3. Return the Result of the second step (2 ).

In ES3, the specification document does not summarize the Internal Attributes of [[class]. However, we can make statistics by ourselves, there are 10 types of native [[class] internal attribute values. they are: "Array", "Boolean", "Date", "Error", "Function", "Math", "Number", "Object", "RegExp ", "String ". so Object. prototype. the output result of toString () is the string [object Array], [object Boolean] in this format.

In ES5.1, Object. prototype. the toString method and the definition of the [[class] internal attribute also have some changes, Object. prototype. the toString method specifications are as follows:

Object. prototype. toString ()
When the toString method is called, the following steps are performed:

1. If the value of this is undefined, "[object Undefined]" is returned.
2. If the value of this is null, "[object Null]" is returned.
3. Make O the result of calling ToObject (this.
4. Make the class the value of the internal attribute [[Class] of O.
5. Return the new string after the three strings "[object", class, and "]" are connected.

It can be seen that step 1, step 3, step 1, and step 2 are new rules, which are special because "Undefined" and "Null" do not belong to the value of the [[class] attribute. According to statistics, the return types include "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math ", "Number", "Object", "RegExp", "String" has two more types than ES3: [[class] of the arguments Object to "Arguments ", instead of the previous "Object", there are multiple global Object JSON whose [[class] value is "JSON ".

Finally, I would like to remind you that Object. prototype. toString (). in the [object class] returned by call (param), the first letter of the class is in upper case, such as JSON, and even in upper case. Therefore, you can convert the class to lower case when making a decision to avoid errors. prototype. toString (). call (param ). toLowerCase.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.