Today I saw a blog by situ, which is a deep copy of JavaScript. It involves a method for determining the type. The analysis 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;} // each logic and operation is enclosed in parentheses in the original text, but according to the operator priority, parentheses can be omitted // The first line declares undefined, to improve performance, you do not have to query undefined in the top-level scope.
As explained in the ECMA-262, object. Prototype. tostring () returns the type of the object instance in the format of "[object", class, and "]" string.
Therefore, slice is used to extract the 'class' value, that is, the type value.
Among them, null and undefined are exceptions, because they return
[Object object] in IE
Standard browser [Object window].
Therefore, let alone make a judgment.
VaR is = function (OBJ, type) {var tostring = object. prototype. tostring; return OBJ = NULL & type = 'null' | OBJ = undefined & type = 'undefined' | tostring. call (OBJ ). slice (8,-1) === type ;}