JavaScript data type and type detection functions-MOOC ArraysSimilar programming exercises

Source: Internet
Author: User

JavaScript data type and type detection functions-MOOC ArraysSimilar programming exercises
Basic Type string
Number
Boolean
Function
Undefined
Object (null, Array, Date, window) implicit conversion with "=" num-string var str = num + ''string-num var num = str-0
"=" For implicit conversion and determination (that is, to determine the value in the variable)
"=" Directly determines (whether it is a thing) (NaN! = NaN null === null undefined === undefined new obj! = New obj [1, 2]! = [1, 2]) when the packaging class calls an attribute of the basic type (String Boolean Number), the basic type is automatically converted into a packaging class of an Object, but note: after this statement is executed, the packaging class is automatically destroyed and cannot be accessed later. (Generally, only values are not assigned or modified, for example, arr1.length)

Type check typeof () return type understand this sentence: typeof only returns 6 basic types !!!!! For example, NaN-number[1, 2]-obj Null-obj Date-obj Window-obj
To make a precise judgment, you need to use the following:

Null:

Null = null or Object. prototype. toString. apply (arr2 [I]) = '[object Null]'

Date:

Object. prototype. toString. apply (arr2 [I]) = '[object Date]'

Window: Object. prototype. toString. apply (arr2 [I]) === '[object global]'

A instanceof B return true/false understand this sentence:
JudgeObjectBelongB or its subclassInstance
If a is B, true is returned (for example, a is childern, B is human, and a inherits B)
This method cannot be used between different iframe or different windows.
Note: var a = "tt123" a instanceof String => false is because a is var and String is a class. Generally, typeof is used to determine the object type. prototype. toString. apply () ie678 judge null and undefined invalidation (returned '[object Object]') exercise http://www.imooc.com/code/5760

* ** =================================================== =====================* In the index.html file, write the arraysSimilar function to determine whether the two input arrays are similar. Specific requirements: * 1. The member types in the array are the same, and the order can be different. For example, [1, true] is similar to [false, 2.*2. The length of the array is the same. * 3. type judgment range, which must be differentiated: String, Boolean, Number, undefined, null, function, date, window. ** if all of the preceding conditions are met, the system returns "decision result: Pass"; otherwise, the system returns "decision Result: No pass ". * ===================================================== =================* // ** Param1 Array * param2 Array * return true or false */function arraysSimilar (arr1, arr2) {if (arr1 instanceof Array & arr2 instanceof Array) {// first judge whether the input is an Array if (arr1.length = arr2.length) {// judge the Array length console. log ("same-length"); console. log (arr1); console. log (arr2); // starts to judge whether the array is similar to return sameLengthArraysSimilar (arr1, arr2);} else {// returns falsereturn false if the length of the two arrays is different ;}} else {// The input parameter is not an array and falsereturn false is returned ;}} /*** determine whether two equal-length arrays are similar * traverse arrays * Whether the number of various types of elements in arr1 is the same as that of various types of elements in arr2 * @ param {array} arr1 Array 1 * @ param {Array} arr2 Array 2 * @ return {true, false} */function sameLengthArraysSimilar (arr1, arr2) {var numInArr1 = 0; var numInArr2 = 0; var booleanInArr1 = 0; var booleanInArr2 = 0; var funInArr1 = 0; var funInArr2 = 0; var undefinedInArr1 = 0; var comment = 0; var stringInArr1 = 0; var stringInArr2 = 0; var nullInArr1 = 0; var nullInArr2 = 0; var dateInArr1 = 0; var dateInArr2 = 0; var windowInArr1 = 0; var windowInArr2 = 0; for (var I = 0; I <arr1.length; I ++) {if (typeof arr1 [I] = 'number') {numInArr1 ++;} else if (typeof arr1 [I] = 'boolean ') {booleanInArr1 ++;} else if (typeof arr1 [I] === 'function') {funInArr1 ++ ;} else if (typeof arr1 [I] === 'undefined') {undefinedInArr1 ++;} else if (typeof arr1 [I] === 'string ') {stringInArr1 ++;} else if (typeof arr1 [I] === 'object') {if (object. prototype. toString. apply (arr1 [I]) ===' [object Null] ') {nullInArr1 ++;} else if (Object. prototype. toString. apply (arr1 [I]) = '[object Date]') {dateInArr1 ++;} else if (Object. prototype. toString. apply (arr1 [I]) = '[object global]') {windowInArr1 ++;} if (typeof arr2 [I] = 'number ') {numInArr2 ++;} else if (typeof arr2 [I] === 'boolean') {booleanInArr2 ++ ;} else if (typeof arr2 [I] === 'function') {funInArr2 ++;} else if (typeof arr2 [I] === 'undefined ') {undefinedInArr2 ++;} else if (typeof arr2 [I] === 'string') {stringInArr2 ++ ;} else if (typeof arr2 [I] === 'object') {if (object. prototype. toString. apply (arr2 [I]) ===' [object Null] ') {nullInArr2 ++;} else if (Object. prototype. toString. apply (arr2 [I]) ===' [object Date] ') {dateInArr2 ++;} else if (Object. prototype. toString. apply (arr2 [I]) ===' [object global] ') {windowInArr2 ++ ;}} console. log ("num ---" + numInArr1); console. log ("num ---" + numInArr2); console. log ("boo ---" + booleanInArr1); console. log ("boo ---" + booleanInArr2); console. log ("null ---" + nullInArr1); console. log ("null ---" + nullInArr2); console. log ("window ---" + windowInArr1); console. log ("window ---" + windowInArr2); console. log ("date ---" + dateInArr1); console. log ("date ---" + dateInArr2); console. log ("string ---" + stringInArr1); console. log ("string ---" + stringInArr2); console. log ("fun ---" + funInArr1); console. log ("fun ---" + funInArr2); console. log ("undefined ---" + undefinedInArr1); console. log ("undefined ---" + undefinedInArr2 ); if (numInArr1 = numInArr2 & region = feature & funInArr1 = funInArr2 & region = feature & stringInArr1 = stringInArr2 & nullInArr1 = feature & dateInArr1 = = dateInArr2 & windowInArr1 = windowInArr2) {console. log ('================================== true '); return true;} else {console. log ('================================== false '); return false ;}}




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.