Home > Web Front-end > JS Tutorial > JavaScript type judgment code analysis_javascript skills

JavaScript type judgment code analysis_javascript skills

WBOY
Release: 2016-05-16 18:31:07
Original
859 people have browsed it
Copy code The code is as follows:

var is = function(obj,type){
var toString =Object.prototype.toString,undefined;
return obj===null&&type==='Null'||
obj===undefined&&type==='Undefined'||
toString.call(obj ).slice(8,-1)===type;
}
//In the original text, there are parentheses surrounding each logical AND operation, but according to the operator priority, the parentheses can be omitted
// The first line declares undefined. My personal understanding is to improve performance and avoid querying undefined in the top-level scope.


According to the explanation in ECMA-262, Object.prototype.toString() , will return the type of object instance and return a string in the format "[object ", class, and "]".
So intercept the 'class' value through slice, which is the type value.
The exceptions are null and undefined, because they return
[object Object] in IE
Standard browser [object Window].
So make a separate judgment.
Related articles: javascript deep copy
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template