The difference between typeof and instanceof and === in js
The difference between typeof and instanceof and === in js typeof: used to judge number/string/boolean/underfined type/function, can not judge: null and object, can not distinguish object and Array instanceof: judge specific object type = ==: Used to judge undefined and null //Five basic types var num=1; var str=”abc”; var bl=true; var nu=null; var undef=undefined; //three special types var obj=new Object(); var arr2=[“1”,2,true]; var fun=function () { } write(“——-typeof———–“) write(num,typeof num);//1 number write(str,typeof str);//abc string write(bl,typeof bl);//true boolean write(nu,typeof nu);//null object write(undef, typeof undef)//undefined undefined write(obj, typeof obj);//[object Object] object write(arr2,typeof arr2);//1,2,true object write(“———–===———–“) write(num,typeof num===”number”);//1 true write(str, typeof str===”string”);//abc true write(bl,typeof bl===”boolean”);//true true write(nu,typeof nu===”object”);//null true write(undef, typeof undef===”undefined”)//undefined true write(obj, typeof obj===”object”);//[object Object] true write(arr2,typeof arr2===”object”);//1,2,true true write(fun, typeof fun===”function”);//function () { } true write(“———instanceof—————“) write(obj, obj instanceof Object)//[object Object] true write(arr2,arr2 instanceof Array);//1,2,true true write(arr2,arr2 instanceof Object);//1,2,true true write(fun, fun instanceof Function)//function () { } true write(fun, fun instanceof Object)//function () { } true The above is the whole content of typeof, instanceof and === in js. Related reference: 1024programmer.com The above are typeof and instanceof and === in js For more details about the difference, please pay attention to other related articles on 1024programmer.com!
Three implementations of JS deep copy
Three implementations of JS deep copy 1. Convert the object into a JSON string, and then convert it into a native JS object; //_tmp and result are independent of each other, have no connection, and have their own storage space. let deepClOne = function (obj) { let _tmp = JSON.stringify(obj);//Convert the object to json string form let result = JSON.parse(_tmp);//Convert the converted string to a native js object return result; }; let obj1 = { weiqiujaun: { age: 20, class: 1502 }, liuxiaotian: { age: 21, class: 1501 } }; let test = deepClone(obj1); console.log(test); 2. Use the for loop in JS to traverse and copy; function deepClone(obj) { let result = typeof obj. splice === “function” ? [] : {}; if (obj && typeof obj === 'object') { for (let key in obj) { if (obj[key] && typeof obj[key] === 'object') { result[key] = deepClone(obj[key]);//If the attribute value of the object is object, call deepClone recursively, that is, copy a value object to the corresponding value of the new object. } else { result[key] = obj[key];//If the attribute value of the object is not object, directly copy each key value of the parameter object to the key-value pair corresponding…
How to remove css attribute in js
Use document.getElementById(“objid”).className=”” to clear the style; The className attribute sets or returns the class attribute of the element. You can love css styles more by changing the class attribute. In some browsers (such as Chrome), it will return whatever value you assign to it. It is more painful in IE, it will format the output, capitalize the attributes, change the order of the attributes, and remove the last semicolon Example: Zhang San WeChat ID: zhangsan Department: Security Department Information List add information Message List 10 Message List 10 Message List 10 Delivery List 10 Notification List 10 Add information Add a message Add a comment Add communication Add notification
How to convert string to array in js
How does js convert strings to arrays? The function of js string to array conversion is “split()”, and its usage is as follows string.split( separator,limit) Parameter value parameter description separator Optional. A string or regular expression to split the string Object from where specified by this parameter. limit Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length. First create a new index.html file and add the following code: Then run the index.html file in the browser to view the console Related reference: JS Tutorial The above is the detailed content of how js converts strings into arrays. For more, please pay attention to other related articles on 1024programmer.com!
How does js realize page jump
First create an html file and fill in the following code: window.location.href = 'nextPage .php’; Then run the file on the browser to realize the page jump. Related reference: JS manual The above is the detailed content of how js realizes page jumping, please pay attention to other related articles on 1024programmer.com for more information!
How to clear the timer in js
1. Setting the timer The window object provides two methods to achieve the effect of the timer, are window .setTimeout() and window.setInterval. The former can make a piece of code run after a specified time; the latter can make a piece of code run once every specified time. Their prototypes are as follows: window.setTimeout(code,millisec); var i = 0 ; //Set the timer (loop to execute) var timeId = setInterval(function () { i++; console.log(&# 39; Scheduled operation: & # 39; + i + & # 39; times & # 39;) }, 500) //Clear timer my$('btn').Onclick= function () { window.clearInterval(timeId) } window.setInterval(code,millisec); var i = 0; //Set Timer (one shot timer) var timeId = setTimeout(function () { i++; console.log(&# 39; Scheduled operation: & # 39; + i + & # 39; times & # 39;) }, 500) //Clean up the timer (although this timer is only once, it must be cleaned up, which can not only release the memory, but also facilitate the judgment of the following code.) my$('btn').Onclick= function () { window.clearTimeout(timeId) } Among them, code can be a piece of code enclosed in quotation marks, or a function name. When the specified time is reached, the system will automatically call…
JS judges whether an array contains a certain value
Optional integer parameter. Specifies the position in the string to start searching. Its valid values are 0 to stringObject.length – 1. If this parameter is omitted, the search will start from the first character of the string. Note: The indexOf() method is case sensitive! Note: If the string value to be retrieved is not present, the method returns -1. Specific usage: First create an html file and add the following code: Then run the html file in the browser Finally check the running results, “-1” means that this value does not exist; Related reference:JS Tutorial The above is the detailed content of JS judging whether an array contains a certain value. For more information, please pay attention to other related articles on 1024programmer.com !
js remove duplicate values in array
function fun(arr){ let newsArr = []; for (let i = 0; i You can also use the splice method to remove duplicate values. Idea: This method is a bit like bubbling two layers of loops, the outer loop traverses the array, and the inner loop compares values , if there is the same, use splice to remove and return the processed array. Specific code: function fun(arr ){ for (let i = 0; i Recommended tutorial: js introductory tutorial The above is the detailed content of js to remove duplicate values in the array. For more, please pay attention to other related articles on 1024programmer.com!
The difference between JavaScript and Dart
What is Javascript? Javascript is often referred to as a browser scripting language, but it has also spread to many server-side and mobile application development environments. JS has been around for almost 20 years and it is safe to say that it is indeed a mature and stable programming language. JS became more and more popular after Facebook released React and React Native frameworks. Javascript has its own package managers such as NPM and Yarn. Even though Javascript is preferred and popular now, there are still some mixed reviews in the programming community. However, it’s safe to say that Javascript’s popularity is undeniable, as it contains nearly 2 million questions tagged on StackOverFlow. What is Dart? Dart is a language optimized by Google for client-side optimization for fast applications on various platforms. Google initially used it as an in-house programming language to build web, server and mobile applications. Although Dart has been around since 2011, it became popular after Google announced Flutter for cross-platform mobile app development. The only reason is that Flutter is completely based on Dart. Therefore, mobile developers must learn Dart to start using Flutter. Dart compiles source code, similar to other programming languages such as C.…