How do you type an object in javascript

  • 2020-03-27 00:07:49
  • OfStack

Recently I was reading John Resig's book Pro  javascript   Techniques, which deals with how to do javascript type judgments. This article describes two ways to use typeof and constructor. It is a slight pity that as the author of jquery, he did not introduce the type determination method used by jquery. But it doesn't matter. Let me summarize it for you.

Here I first recommend a great online editor: (link: http://jsfiddle.net/) . It provides versions of three major js frameworks: jquery, mootools, prototype, and YUI, which you can use directly when you need to write simple js tests. Saves you the step of opening the editing software and creating various types of files. After editing the code, click the [Run] button and you're done.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201311/201311111112482.png ">

1. The typeof

Typeof is the most commonly used method for type determination. Its advantages are simple and easy to remember, while its disadvantages are that it cannot judge object, null, array, regexp and custom objects well.

Here is my test code:


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);

Code running results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201311/201311111112483.png ">

2. The constructor

The test code is much the same as before, except that typeof is used instead of xxx. constructor.


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(str.constructor);
console.log(arr.constructor);
console.log(num.constructor);
console.log(bool.constructor);
console.log(obj.constructor);
console.log(reg.constructor);
console.log(fn.constructor);
console.log(user.constructor);
console.log(nullObj.constructor);
console.log(undefinedObj.constructor);

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201311/201311111112484.png ">

Run to   The console. The log (nullObj. Constructor); , the browser says: Uncaught TypeError: Cannot read property 'constructor' of null. A similar problem occurs with console.log(undefinedobj. constructor); Above: Uncaught TypeError: Cannot read property 'constructor' of undefined.

3. The Object. The prototype. ToString. Call ()

The test 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');
var toString=Object.prototype.toString;
console.log(toString.call(str));
console.log(toString.call(arr));
console.log(toString.call(num));
console.log(toString.call(bool));
console.log(toString.call(obj));
console.log(toString.call(reg));
console.log(toString.call(fn));
console.log(toString.call(user));
console.log(toString.call(nullObj));
console.log(toString.call(undefinedObj));

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201311/201311111112485.png ">

The console. The log (toString. Call (user)); [object object], can't make further judgment.

conclusion

We run the following code in the browser:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201311/201311111112486.png ">

Null and undefined because there is no toString() method, so it will report an error, so we'll leave them alone. As for the other Object, and through the toString () returns the contents and the use of the Object. The prototype. ToString. The content of the call () returns the difference is very big. This is because the Object. The prototype. The toString () method is designed to return the Object type. String, Array, Boolean, Regexp, Number, and Function all inherit from Object, which also inherits the Object prototype method toString(), but they all override toString(). Perform XXX. The toString () is rewritten method, used in the results returned will naturally and Object. The prototype. ToString. The result of the call ().

Through the above examples, you must have a deeper understanding of these three ways, familiar with their advantages and disadvantages, and then can according to their own needs to choose the right way. . It is recommended to use the Object prototype. ToString. Call () method, because he can solve most of the condition judgment, in case the return value for [Object Object], then use the constructor auxiliary judgment, whether custom Object.


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(str.toString());
console.log(arr.toString());
console.log(num.toString());
console.log(bool.toString());
console.log(obj.toString());
console.log(reg.toString());
console.log(fn.toString());
console.log(user.toString());
console.log(nullObj.toString());
console.log(undefinedObj.toString());


Related articles: