26、图片懒加载
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img {
width: 200px;
height: 200px;
display: block;
margin-bottom: 20px;
}
</style>
</head>
<body>
<img id="1" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="2" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="3" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="4" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="5" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="6" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="7" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="8" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="9" src="../img/default.jpg" data-src="../img/1.jpg" />
<img id="10" src="../img/default.jpg" data-src="../img/1.jpg" />
<script>
let imgList = [...document.querySelectorAll('img')];
function imglazyLoad() {
imgList.forEach((item, index) => {
if (item.dataset.src === '') return;
let rect = item.getBoundingClientRect();
debugger
if (rect.top < window.innerHeight) {
item.src = item.dataset.src;
}
})
}
function throttle(fn, delay) {
let timer;
let prevTime;
return function (...args) {
const currTime = Date.now();
const context = this;
if (!prevTime) prevTime = currTime;
clearTimeout(timer);
if (currTime - prevTime > delay) {
prevTime = currTime;
fn.apply(context, args);
clearTimeout(timer);
return
}
timer = setTimeout(function () {
prevTime = Date.now();
timer = null;
fn.apply(context, args);
}, delay)
}
}
document.addEventListener('scroll', throttle(imglazyLoad, 200));
</script>
</body>
</html>
27、事件总线
<script>
class EventEmitter {
constructor() {
this._event = {}
}
on(name, fn) {
if (this._event[name]) {
this._event[name].push(fn);
} else {
this._event[name] = [fn];
}
}
off(name, fn) {
let tasks = this._event[name];
if (tasks) {
const index = tasks.findIndex(f => f === fn);
if (index >= 0) {
tasks.splice(index, 1);
}
}
}
emit(name, once = false, ...args) {
if (this._event[name]) {
let tasks = this._event[name].slice();
for (let fn of tasks) {
fn(...args);
}
if (once) {
delete this._event[name];
}
}
}
}
let eventBus = new EventEmitter();
let fn1 = function (name, age) {
console.log(`${name} ${age}`);
}
let fn2 = function (name, age) {
console.log(`hello, ${name} ${age}`);
}
debugger
eventBus.on('aaa', fn1);
eventBus.on('aaa', fn2);
eventBus.emit('aaa', false, '布兰', 12);
</script>
28、数组forEach
<script>
Array.prototype.myForEach = function (callback, thisArg) {
debugger
if (this == null) {
throw new TypeError('this is null or not defined');
}
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
const O = Object(this);
const len = O.length >>> 0;
let k = 0;
while (k < len) {
if (k in O) {
callback.call(thisArg, O[k], k, O);
}
k++;
}
}
debugger
const arr = ['name','age','gender'];
const fruits = ['apple', 'banane', 'butttler'];
arr.myForEach((item ,index) => {
item = item + index;
})
</script>
29、Object.create实现
<script>
Object.create2 = function (proto, propertyObject) {
if (typeof proto !== 'object' && typeof proto !== 'function') {
throw new TypeError('Object prototype may only be an Object or null.');
}
if (propertyObject === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
function F() { };
F.prototype = proto;
const obj = new F();
if (propertyObject !== undefined) {
Object.defineProperties(obj, propertyObject);
}
if (proto === null) {
obj.__proto__ = null;
}
return obj;
}
</script>
Object.create2 = function (proto, propertyObject) {
if (typeof proto !== 'object' && typeof proto !== 'function') {
throw new TypeError('Object prototype may only be an Object or null.');
}
if (propertyObject === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
const obj = {};
Object.setPrototypeOf(obj, proto);
if(propertyObject !== undefined) {
Object.defineProperty(obj, propertyObject);
}
return obj;
}
30、Object.assgin实现
<script>
Object.assign2 = function (target, ...source) {
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object')
}
let ret = Object(target);
source.forEach(function (obj) {
if (obj != null) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
ret[key] = obj[key]
}
}
}
})
return ret
}
const man = { name: 'jjwu' }
Object.assign2(man, {});
</script>