JavaScript data type, type detection function--MU lesson network Arrayssimilar Programming Exercise

Source: Internet
Author: User
Tags object object

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
"= =" after the implicit conversion judgment (that is, determine the value of the variable)
"= = =" Direct Judgment (judgment is not a thing)(Nan!=nanNull===nullundefined===undefinedNew obj! = new obj[up]! = [+])When a wrapper class calls an attribute of the base type (String Boolean number), the base type is automatically converted to a wrapper class of object, but note that the wrapper class is automatically destroyed after the statement is executed and cannot be accessed later. (generally only do the value does not do the assignment and the change value, such as Arr1.length)

Type detection typeof () return typeunderstand this sentence: typeof only returns 6 BASIC types!!!!! For example:Nan-number [1,2]-obj Null-obj Date-obj Window-obj
to make a fine judgment, you need to use the following:

Null:

Null===null or objectprototype.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/falseunderstand this sentence:
Determine if the object belongs to an instance of Class B or its subclasses
If A is B, it returns true (e.g. A is childern,b is human,a inherits B)
This method cannot be used between different iframe or different Windows
Note: Var a= "tt123" a instanceof String = FalseThe reason is that A is Var, and string is a class, generally this basic type should be judged by TypeOfobject.prototype.toString.apply ()ie678 null and undefined invalidation (return ' [Object Object] ') Exercises http://www.imooc.com/code/5760

* * ===================================================== * In the index.html file, write the Arrayssimilar function to determine if the incoming two 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 consistent. * 3. The type of judgment range that needs to be distinguished: String, Boolean, number, undefined, null, function, date, window. * * When all of the above are satisfied, then return "decision Result: Pass", otherwise return "decision Result: Do not pass".  * ===================================================== *//** param1 array* param2 array* return True or false*/function Arrayssimilar (arr1, arr2) {if (arr1 instanceof array && arr2 instanceof Array) {//first determine if the incoming is an array if (arr1.length = = A Rr2.length) {//Judgment array length Console.log ("Same-length"); Console.log (arr1); Console.log (ARR2);//Start judging if the inside of the array is similar to return Samelengtharrayssimilar (arr1, arr2);} else{//two arrays of different lengths return Falsereturn false;}} else {//The parameter passed in is not an array return Falsereturn false;}}  /** * To determine if two equal-length arrays are similar within the array * iterate through the array * arr1 the number of elements in the various types in the ARR2 is the same as the number of elements in the various types of the element * @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 UNDEFINEDINARR2 = 0;va R STRINGINARR1 = 0;var STRINGINARR2 = 0;var nullInArr1 = 0;var NULLINARR2 = 0;var dateInArr1 = 0;var DATEINARR2 = 0;var WI NDOWINARR1 = 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 (Typ EOF Arr1[i] = = = ' undefined ') {undefinedInArr1 + +;} else if (typeof arr1[i] = = = ' String ') {stringInArr1 + +;} else if (typeof ar R1[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 (Typeo F 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 && booleaninarr1==booleaninarr2 && FUNINARR1==FUNINARR2 && undefinedinarr1==undefinedinarr2 && stringinarr1==stringinarr2 && NULLINARR1==NULLINARR2 && dateinarr1==dateinarr2 && windowinarr1==windowinarr2) {console.log (' ===== ===========================true '); return true;} Else{console.log (' ================================false '); return false;}}




JavaScript data type, type detection function--MU lesson network Arrayssimilar Programming Exercise

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.