1.将一个嵌套数组拆分
var arrs = [1,2,3,[[1,2,3,4,5],5,6],[7,8,9]];
function single(newarr,arr){
newarr = newarr || [];
if(!arr)return;
for(let i = 0 ;i < arr.length;i++){
if(Object.prototype.toString.call(arr[i]) === '[object Array]'){
single(newarr,arr[i]);
}else{
newarr.push(arr[i]);
}
}
return newarr;
}
var singlearr = single([],arrs);
console.log(singlearr);
2.两列布局
a.父元素宽度设置为100%;左侧原色width固定,右侧元素width 为auto;
.container{width:100%;height:200px}
.left{width:200px;height:100%;float:left;background: #00f}
.right{width:auto;height:100%;background: #ff0}
b.flex 父元素设置为display:flex;左侧宽度固定,右侧flex值设置为1
.container{display:flex;width:100%;height:200px}
.left{width:200px;height:100%;background: #00f}
.right{flex:1;height:100%;background: #ff0}
c.position定位实现 父元素relative,子元素absolute
.container{position:relative;width:100%;height:200px}
.left{position:absolute;height:100%;background: #00f}
.right{position:absolute;left:200px;height:100%;background: #ff0}
4.实现继承
function extend(fun,options){
function child(){
fun.call(this);
for (var opt in options) {
this[opt] = options[opt];
}
}
child.prototype = new fun();
child.prototype.constructor = child;
return child;
}
function parent(){
this.name = 'parent';
}
parent.prototype.sayname = function(){
alert(this.name);
}
var fun1 = extend(parent,{name:'child'});
var cc = new fun1();
cc.sayname();