清除浮动 (写在有浮动的元素容器上去除浮动影响,就不用在后面元素上写clear:both;)
.clearfix:after{
content:'' ;
display:table;
clear:both;
}
手写深拷贝(考察数据类型)
function deepClone ( obj = { } ) {
if ( obj == null || typeof obj !== 'object' ) {
return obj
}
let result
if ( obj instanceof Array ) {
result = [ ]
} else {
result = { }
}
for ( let key in obj) {
if ( key. hasOwnProperty ( key) ) {
result[ key] = deepClone ( obj[ key] )
}
}
return result
}
手写jQuery(考察原型原型链)
class jQuery {
constructor ( select ) {
const result = document. querySelectorAll ( selector)
const length = result. length
for ( let i = 0 ; i< length; i++ ) {
this [ i] = rusult[ i]
}
this . length = length
}
get ( index) {
return this [ index]
}
each ( fn ) {
for ( let i = 0 ; i < this . length; i++ ) {
const elem = this [ i]
fn ( elem)
}
}
on ( type, fn ) {
return this . each ( elem => {
elem. addEventListener ( type, fn, false )
} )
}
}
jQuery. prptotype. dialog = function ( info ) {
alert ( info)
}
const $p = new jQuery ( 'p' )
$p. dialog ( )
class myJQuery extends jQuery {
construtor ( selector ) {
super ( selector)
}
addClass ( className ) {
}
style ( data ) {
}
}
手写bind函数(考察作用域和闭包,必包其实就是作用与的特殊情况,归根结底还是作用域)
Function . prototype. bind1 = function ( ) {
const args = Array . prototype. slice . call ( arguments)
const t = args. shift ( )
const self = this
return function ( ) {
return self . apply ( t, args)
}
}
fn. bind1 ( { x : 100 } , 10 , 20 )
创建10个标签,点击的时候弹出对应的序号(考察作用域)
let a
for ( let i= 0 ; i< 10 ; i++ ) {
a = document. createElement ( 'a' )
a. innerHTML = i + '<br>'
a. addEventListener ( 'click' , function ( e ) {
e. preventDefault ( )
alert ( i)
} )
document. body. appendChild ( a)
}
手写用promise加载一张图片(考察异步promise)
function loadImg ( src ) {
const p = new Promise (
( resolve, reject ) => {
const img - document. createElement ( 'img' )
img. onload = ( ) => {
resolve ( img)
}
img. onerror = ( ) => {
const err = new Error ( ` 图片加载失败 ${ src} ` )
reject ( err)
}
img. sec = src
}
)
return p
}
loadImg ( url1) . then ( img1 => {
console. log ( img1. width)
return img1
} ) . then ( img1 => {
console. log ( img1. height)
return loadImg ( url2)
} )