To judge the types of data in JS typeof and 0bject.prototype.tostring explanation _ basic knowledge

Source: Internet
Author: User

1, typeof (Param) Returns the type of Param (string)

This method is the definition of JS in the global approach, but also compilers are the most commonly used method, the advantage is that the use of simple, good memory, the disadvantage is not good to judge object, NULL, array, regexp and custom objects.

Sample code:

Copy Code code 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 (FN) (typeof);
Console.log (typeof (user));


The results are:
Copy Code code as follows:

String
Object
Number
Boolean
Object
Object
Undefined
Object
function
Object

2, Object.prototype.toString (). Call (param) Returns the type of param (string, format is [Object class])

This method supports most types of judgments, and this method is used for the type judgments of jquery encapsulation. Maybe some people 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 instead of a, and then perform a fun function, write an example:

Copy Code code 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);


Run the result, the output is class2, not Class1, which is equivalent to the method inheritance.
So, the meaning of Object.prototype.toString () call (param) is actually, param.prototype.toString (), So why don't we just write param.prototype.toString () and use Call () to get around it, and see below for a second.

2) Object.prototype.toString ()

What is Object? , Script56.chm (the official m$ tutorial) said: Obect provides common functionality for all JScript objects, in fact, object is the ancestor of all JS objects, is a concept, all the objects in JS is an instance of object, Then the different objects rewrite their own independent methods. And prototype, there is no need to investigate too deep, it is to return a reference to the prototype, but you can dynamically add methods and attributes to the prototype
A small example

Copy Code code 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 ();

Then the class and prototype test class are output separately, and the Shownamecontact function is not defined in the constructor class (), and by prototype we can dynamically add functions to the object prototype, The example of new will naturally be available. So the meaning of Object.prototype.toString () is to execute the ToString method in the ancestor of object.

So what does ToString () do? Many JS manuals define the ToString () function in this way:
The ToString () method converts a logical value to a string and returns the result, which is the syntax: booleanobject.tostring (). As I said earlier, the objects in JS are inherited object, and these objects have functions that have been customized or that have refactored some of the functions of object, and they have all rewritten the ToString () function. So we don't want to write the param.prototype.toString () directly in 1, so we're going to perform the toString () function that param ourselves to rewrite.

Well, to the critical moment, toString () What is it, what is the role?

In ES3, the specification for the Object.prototype.toString method is as follows:

Object.prototype.toString ()

When the ToString method is invoked, the following procedure is performed:

1. Gets the value of the [[Class]] property of the This object.

2. Calculates the three string "[object", the first step results result (1), and "]" the new string after the connection.

3. Returns the result of the operation of the second step (2).

In the ES3, the specification document does not summarize [[class]] internal properties A total of several, but we can count ourselves, the original object's [[Class]] internal properties of the value of a total of 10. respectively: "Array", "Boolean", "Date", "Error , "Function", "Math", "number", "Object", "RegExp", "string". So the output of Object.prototype.toString () is a string of this format [object Array],[object Boolean].

In ES5.1, there are some changes in the definition of the Object.prototype.toString method and [[class]] internal properties, in addition to the more detailed specification written, and the specification of the Object.prototype.toString method is as follows:

Object.prototype.toString ()
When the ToString method is invoked, the following procedure is performed:

1 returns "[Object Undefined]" If the value of this is Undefined.
2 returns "[Object NULL]" If the value of this is null.
3 Let o become the result of calling Toobject (this).
4 Let Class be the value of the internal property [[Class]] of O.
5 returns a new string of three strings "[Object, class, and]" after the connection.

It can be seen that more than ES3 1,2,3 step. 1th, 2 steps belong to the new rule, which is special because "Undefined" and "Null" do not belong to the value of the [[Class]] property. Statistically, the types that can be returned are "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "number", "Object", "RegExp", "S Tring "More than ES3 2 kinds of Arguments objects [[class]] became" Arguments ", not the previous" object ", there is a number of global object JSON, its [[Class]] value is" JSON ".

The final reminder is that Object.prototype.toString (). Call (param) Returns the first letter of class in [Object class], like JSON, which is even uppercase, so you can all switch to lowercase when you're judging. To prevent errors, Object.prototype.toString (). Call (param). toLowerCase ().

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.