总之一句话:技不如人

js怎样添加、移除、替换、之前插入、之后插入、复制、创建和查找节点

appendChild()
removeChild()
replaceChild()
insertBefore()
insertAfter()
cloneNode()

document.getElementsByTagName(“”) //通过标签名称
document.getElementsByName(“”) //通过元素的Name属性的值
document.getElementById(“”) //通过元素Id,唯一性
document.getElementsByClassName(“”); //通过类查找
document.querySelector(“”)

https://blog.csdn.net/webzrh/article/details/70198265

JS中typeof与instanceof的区别 Object.prototype.toString.call()

typeof 一般只能返回如下几个结果:
number,boolean,string,function,object,undefined。我们可以使用 typeof 来获取一个变量是否存在,如 if(typeof a!=”undefined”){alert(“ok”)},而不要去使用 if(a) 因为如果 a 不存在(未声明)则会出错,对于 Array,Null 等特殊对象使用 typeof 一律返回 object,这正是 typeof 的局限性。
这种方法对于一些常用的类型来说那算是毫无压力,比如Function、String、Number、Undefined等,但是要是检测Array的对象就不起作用了。 利用typeof除了array和null判断为object外,其他的都可以正常判断

instanceof
instanceof 用于判断一个变量是否某个对象的实例,如 var a=new Array();alert(a instanceof Array); 会返回 true,同时 alert(a instanceof Object) 也会返回 true;这是因为 Array 是 object 的子类。再如:function test(){};var a=new test();alert(a instanceof test) 会返回
谈到 instanceof 我们要多插入一个问题,就是 function 的 arguments,我们大家也许都认为 arguments 是一个 Array,但如果使用 instaceof 去测试会发现 arguments 不是一个 Array 对象,尽管看起来很像。
另外:
测试 var a=new Array();if (a instanceof Object) alert(‘Y’);else alert(‘N’);
得’Y’
但 if (window instanceof Object) alert(‘Y’);else alert(‘ N’);
得’N’
所以,这里的 instanceof 测试的 object 是指 js 语法中的 object,不是指 dom 模型对象。
使用 typeof 会有些区别
alert(typeof(window)) 会得 object

http://blog.sina.cn/dpool/blog/s/blog_65d41dff0102wnwl.html?vt=4
https://www.cnblogs.com/ma-shuai/articles/7805264.html

new操作符都干了什么

  1. function Person1(name){
  2. this.name = name;
  3. }
  4. function Person2(name){
  5. this.name = name;
  6. return this.name;
  7. }
  8. function Person3(name){
  9. this.name = name;
  10. return new String(name);
  11. }
  12. function Person4 (name){
  13. this.name = name;
  14. return function () {
  15. }
  16. }
  17. function Person5(name){
  18. this.name = name;
  19. return new Array();
  20. }
  21. const person1 = new Person1("yuer");
  22. //Person1 {name: "yuer"}
  23. const person2 = new Person2("yuer");
  24. //Person2 {name: "yuer"}
  25. const person3 = new Person3("yuer");
  26. //String {0: "y", 1: "u", 2: "e", 3: "r", length: 4, [[PrimitiveValue]]: "yuer"}
  27. const person4 = new Person4("yuer");
  28. //function() {}
  29. const person5 = new Person5("yuer");
  30. //[]

这里给出了5个例子,其实new操作符干了以下三步:
1.先创建了一个新的空对象
2.然后让这个空对象的__proto__指向函数的原型prototype
3.将对象作为函数的this传进去,如果return 出来东西是对象的话就直接返回 return 的内容,没有的话就返回创建的这个对象

对应伪代码:
对于const a = new Foo();,new干了以下事情

const o = new Object();//创建了一个新的空对象o
o.__proto__ = Foo.prototype;//让这个o对象的` __proto__`指向函数的原型`prototype`
Foo.call(o);//this指向o对象
a = o;//将o对象赋给a对象

随机数

  1. Math.random() 为0到1之间的随机数(包括0,不包括1)[0, 1)
  2. Math.round(n) 为n四舍五入后的整数
  3. Math.ceil(n) 为大于等于n的最小整数
  4. Math.floor(n) 为小于等于n的最大整数

min ≤ n ≤ max : Math.round(Math.random()*(max-min)+min)

创建一个长度为10,元素范围为1到10之间的数组

 {
      const arr = []
      for(let i = 0; i < 10; i++){
         const a = Math.random()*10 + 1;
         arr.push(Math.floor(a))
      }
      console.log(arr);
 }

 创建一个长度为100,元素范围为31到60之间的数组(包括31也包括60)

  const arr = []
  for(let i = 0; i < 100; i++){
     const a = Math.random()*29 + 31;
     arr.push(Math.round(a))
  }
  console.log(arr);

https://blog.csdn.net/hope93/article/details/86493013

js数组与字符串的相互转换方法

a = new Array(0,1,2,3,4);
b = a.join("-");      //"0-1-2-3-4"


var s = "abc,abcd,aaa";
ss = s.split(",");// 在每个逗号(,)处进行分解  ["abc", "abcd", "aaa"]
var s1 = "helloworld";
ss1 = s1.split('');  //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]

数组去重

set

function (arr) {
    let a = new Set(arr)
  return [...a]
}

https://www.cnblogs.com/jiayuexuan/p/7527055.html