总之一句话:技不如人
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操作符都干了什么
function Person1(name){this.name = name;}function Person2(name){this.name = name;return this.name;}function Person3(name){this.name = name;return new String(name);}function Person4 (name){this.name = name;return function () {}}function Person5(name){this.name = name;return new Array();}const person1 = new Person1("yuer");//Person1 {name: "yuer"}const person2 = new Person2("yuer");//Person2 {name: "yuer"}const person3 = new Person3("yuer");//String {0: "y", 1: "u", 2: "e", 3: "r", length: 4, [[PrimitiveValue]]: "yuer"}const person4 = new Person4("yuer");//function() {}const person5 = new Person5("yuer");//[]
这里给出了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对象
随机数
- Math.random() 为0到1之间的随机数(包括0,不包括1)[0, 1)
- Math.round(n) 为n四舍五入后的整数
- Math.ceil(n) 为大于等于n的最小整数
- 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]
}
