11、构造函数
<script>
function Pig(name,age) {
this.name = name;
this.age = age;
}
const pig1 = new Pig('wujunjie', 18);
const pig2 = new Pig('huaxia', 17);
console.log(pig1,pig2);
Date.now();
Math.random();
</script>
12、Object内置构造函数
<script>
const o = {
name: 'wujunjie'
}
console.log(o);
const j = new Object();
j.name = 'huaxia';
console.log(j);
const k = new Object({name: 'xiaoxia'});
console.log(k);
const jjwu = { name:'佩奇', age: 6, friend: { name: 'ngb', age: '0.6' }};
const obj = {};
Object.assign(obj,jjwu);
console.log(obj);
jjwu.age = 10;
jjwu.friend.name = 'newNgb';
console.log(obj);
Object.assign(obj, {
name: 'jjwu',
gender: '男',
face: 'smart'
})
console.log(obj);
jjwu.age = 8;
jjwu.name = 'newJjwu';
console.log(obj);
const spec = { size: '40cm*40cm', color: '黑色' };
const str = Object.values(spec).join('/');
console.log(str);
const object1 = { a: 'somestring', b: 42, };
console.log(Object.entries(object1));
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
</script>
13、数组的常用方法
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script>
const arr = [1, 3, 5];
const total = arr.reduce((pre, cur) => {
return pre + cur;
}, 6)
console.log(total);
const newArr = [1, 4, 6, 7].reduce((result, item) => {
item = item * 2;
result.push(item)
return result;
}, []);
const newArr1 = [1, 4, 6, 7].reduce((result, item) => {
item = item * 2;
result.push(item);
return result;
}, []);
console.log(newArr1);
const arr1 = [{name: 'zhangsan',salary: 1000},{name: 'jjwu',salary: 100000},{name: 'nanguabing',salary: 2000}]
const salary = arr1.reduce((pre, cur) => {
const value = pre + cur.salary;
return value;
}, 0)
console.log(salary)
console.log([2,3,4].join('+'));
const objArr1 = [{name: 'jjwu27'}, {name: 'haha'}, {name: 'jjwu27', age: 18}];
console.log(objArr1.find(n => n.name === 'jjwu27'));
console.log([true,true,false,true].every(n => n));
console.log([true,true,true,true].every(n => n));
console.log([true,false,false,false].some(n => n));
console.log([false,false,false,false].some(n => n));
const array11 = ['a', 'b', 'c'];
const array22 = ['d', 'e', 'f'];
const array33 = array11.concat(array22);
console.log(array11, array22, array33);
const array44 = [{name: 'jjwu27'}, 'b', 3];
const array55 = ['d', {age: 8}, 'f'];
const array66= array44.concat(array55);
console.log(array44, array55, array66);
array44[0].age = 9;
console.log(array44, array55, array66);
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
const array111 = [1, 30, 4, 21, 100000];
array111.sort();
console.log(array111);
const numbers = [3, 1, 4, 1, 5];
const sorted = numbers.sort((a, b) => a - b);
console.log(sorted, numbers);
sorted[0] = 10;
console.log(numbers[0]);
const myFish1 = ["angel", "clown", "mandarin", "sturgeon"];
const removed1 = myFish1.splice(2, 0, "drum", "guitar");
const myFish2 = ["angel", "clown", "drum", "mandarin", "sturgeon"];
const removed2 = myFish2.splice(3, 1);
const myFish3 = ["angel", "clown", "drum", "sturgeon"];
const removed3 = myFish3.splice(2, 1, "trumpet");
const myFish4 = ["angel", "clown", "mandarin", "sturgeon"];
const removed4 = myFish4.splice(-2, 1);
const myFish5 = ["angel", "clown", "mandarin", "sturgeon"];
const removed5 = myFish5.splice(2);
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
console.log(animals.slice(2, 4));
console.log(animals.slice(1, 5));
console.log(animals.slice(-2));
console.log(animals.slice(2, -1));
console.log(animals.slice());
const array1 = ['one', 'two', 'three'];
console.log(array1);
const reversed = array1.reverse();
console.log(reversed);
console.log(array1);
console.log([3,4,6,8,4].findIndex(n => n === 4));
console.log([3,4,6,8,4].findIndex(n => n === 5));
const list = document.querySelectorAll('ul li');
console.log(list);
const newList = Array.from(list);
console.log(newList, list);
const a = [1, 2];
a.push(3, 4, 5)
console.log(a);
const b = [1, 2, 3, 4, 5, 6];
b.unshift(-2, -1, 0)
console.log(b);
</script>
</body>
14、字符串的常用方法
<script>
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words);
const chars = str.split('');
console.log(chars);
const strCopy = str.split();
console.log(strCopy);
const str1 = 'Mozilla';
console.log(str1.substring(1, 3));
console.log(str1.substring(2));
const str2 = 'Saturday night plans';
console.log(str2.startsWith('Sat'));
console.log(str2.startsWith('Sat', 3));
console.log(str2.startsWith('urd', 3));
const sentence = 'The quick brown fox jumps over the lazy dog.';
const word = 'fox';
console.log(sentence.includes(word));
console.log(sentence.includes(word, 17))
console.log("Blue Whale".includes("blue"));
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, indexOfFirst + 1)}`);
</script>
15、Number
<script>
console.log(Number('123'));
console.log(Number(''));
console.log(Number('string'));
console.log(Number(true));
console.log(Number(false));
console.log(Number(NaN));
console.log(Number(undefined));
console.log(Number(null));
console.log(typeof NaN);
console.log(Object.prototype.toString.call(NaN));
const price = 12.345;
console.log(price.toFixed(2));
</script>
16、构造函数一些值得注意的地方
<script>
const arr = [1,2,3];
Array.prototype.max = function() {
return Math.max(...this)
}
console.log(arr.max());
function Star() {
}
Star.prototype.sing = function() {
console.log('我会唱歌');
}
Star.prototype.dance = function() {
console.log('我会跳舞');
}
console.log(Star.prototype);
Star.prototype = {
sing: function() {
console.log('我会唱歌');
},
dance: function() {
console.log('我会跳舞');
}
}
console.log(Star.prototype);
Star.prototype.constructor = Star;
console.log(Star.prototype);
</script>
17、几种常见的浅拷贝
<script>
const obj = {
name: 'jjwu27',
age: 18,
grilFriend: {
name: 'xia',
age: 17
}
}
function deepCopy(newObj, oldObj) {
for(let k in oldObj) {
if(oldObj[k] instanceof Array) {
newObj[k] = [];
deepCopy(newObj[k], oldObj[k]);
} else if(oldObj[k] instanceof Object) {
newObj[k] = {};
deepCopy(newObj[k], oldObj[k]);
} else {
newObj[k] = oldObj[k];
}
}
}
const o = {};
deepCopy(o, obj);
o.grilFriend.sing = function() {
console.log('我会唱歌')
}
console.log(o, obj);
</script>
18、异常
<script>
function fn(x, y) {
if(!x || !y) {
throw new Error('缺少参数!')
}
return x + y;
}
try {
const arr = {};
arr.style.name = 'jjwu27';
} catch(err) {
console.log(err);
console.log(err.message);
throw new Error(err);
} finally {
console.log('jjwu27');
console.log('nanguabing');
}
</script>
19、this的一些值得注意的点
<body>
<button>你好</button>
<script>
const arr = [100, 123, 112];
const max = Math.max.apply(null, arr);
const min = Math.min.apply(null, arr);
console.log(max, min);
const btn = document.querySelector('button');
btn.addEventListener('click', function() {
console.log(this);
this.disabled = true;
setTimeout(function() {
console.log(this);
this.disabled = false;
}.bind(this), 2000)
})
</script>
</body>
20、类型判断
let obj = {};
Object.prototype.toString.call(obj) === '[Object Object]'
let obj = {};
obj.constructor === Object
let obj = [];
obj.constructor === Array
let obj = {};
obj instanceof Object
let arr = [];
arr instanceof Object
let obj = {};
typeof obj === 'object';
typeof undefined === 'undefined';
typeof null === 'object';
typeof true === 'boolean';
typeof 123 === 'number';
typeof 'abc' === 'string';
typeof function() {} === 'function';
typeof {} === 'object';
typeof [] === 'object';
21、WeakMap和WeakSet
const wm = new WeakMap();
const obj = {};
wm.set(obj, 'value');
console.log(wm.get(obj));
const ws = new WeakSet();
ws.add(obj);
console.log(ws.has(obj));
const arr1 = Array.of(1, 2, 3);
console.log(arr1);
const str = 'Hello';
const arr = Array.from(str);
console.log(arr);
const nums = [1, 2, 3, 4, 5];
const doubled = Array.from(nums, num => num * 2);
console.log(doubled);
const str = 'abcdefg';
console.log(str.split(''));
console.log(Array.from(str));
Reflect.apply(Array.prototype.filter,str,[(item)=>{
return item;
}])
const str = 'abcdefg';
const arr = [];
for (let index = 0; index < str.length; index++) {
arr.push(str[index]);
}
console.log(arr);
const str = 'abcdefg';
const arr = [...str];
const arr3 = [1, 2, 3, 4, 5];
console.log(arr3.at(2));
const arr4 = [1, [2, [3]]];
console.log(arr4.flat(2));