function checkedType ( target ) {
return Object . prototype. toString . call ( target) . slice ( 8 , - 1 ) ;
}
console. log ( checkedType ( [ ] ) ) ;
console. log ( checkedType ( { } ) ) ;
console. log ( checkedType ( null ) ) ;
console. log ( checkedType ( undefined ) ) ;
function deepCopy ( target ) {
var result;
var targetType = checkedType ( target) ;
if ( targetType === 'Object' ) {
result = { } ;
} else if ( targetType === 'Array' ) {
result = [ ] ;
} else {
return target;
}
for ( let key in target) {
let value = target[ key] ;
if ( checkedType ( value) === 'Object' || checkedType ( value) === 'Array' ) {
result[ key] = deepCopy ( value) ;
} else {
result[ key] = value;
}
}
return result;
}
var obj1 = {
'name' : 'zhangsan' ,
'age' : 18 ,
'language' : [ 1 , [ 2 , 3 ] ,
[ 4 , 5 ]
]
} ;
var obj2 = deepCopy ( obj1) ;
obj2. name = "lisi" ;
obj2. language[ 1 ] = [ "二" , "三" ] ;
console. log ( 'obj1' , obj1) ;
console. log ( 'obj2' , obj2) ;
function isObject ( obj ) {
return ( typeof obj === 'object' && obj !== null ) ;
}
function isEqual ( obj1, obj2 ) {
if ( ! isObject ( obj1) || ! isObject ( obj2) ) {
return obj1 === obj2
}
if ( obj1 === obj2) {
return true
}
if ( Object. keys ( obj1) . length !== Object. keys ( obj2) . length) {
return false
}
for ( const key in obj1) {
let res = isEqual ( obj1[ key] , obj2[ key] ) ;
if ( ! res) {
return false
}
}
return true
}
const obj1 = {
a : 1 ,
b : {
x : 'aa' ,
y : 'bb' ,
z : 'cc'
} ,
c : [ 1 , 2 , 3 ]
}
const obj2 = {
a : 1 ,
b : {
x : 'aa' ,
y : 'bb' ,
z : 'cc'
} ,
c : [ 1 , 2 , 3 ]
}
console. log ( isEqual ( obj1, obj2) ) ;
function debounce ( fn, delay ) {
let timer;
return function ( ) {
clearTimeout ( timer) ;
timer = setTimeout ( ( ) => {
fn . apply ( this , arguments) ;
} , delay) ;
}
}
function throttle ( fn, delay ) {
let flag = true ;
return function ( ) {
if ( ! flag) { return ; }
flag = false ;
setTimeout ( ( ) => {
fn . apply ( this , arguments) ;
flag = true ;
} , delay) ;
}
}
function flatten ( arr ) {
var res = [ ] ;
arr. map ( item => {
if ( Array. isArray ( item) ) {
res = res. concat ( flatten ( item) ) ;
} else {
res. push ( item) ;
}
} ) ;
return res;
}
var arr = [ [ 1 , 2 ] , 3 , [ 4 , 5 , [ 6 , 7 , [ 8 , 9 , [ 10 , 11 ] ] ] ] ] ;
console. log ( flatten ( arr) ) ;
let arr = [ 1 , 2 , 2 , 3 , 'x' , 4 , 3 , 2 , 1 , 'x' ] ;
function unique1 ( arr ) {
return arr. filter ( ( item, index, self ) => {
return self. indexOf ( item) == index
} )
}
function unique2 ( arr ) {
return [ ... new Set ( arr) ] ;
}
function unique3 ( arr ) {
let result = [ ] ;
for ( var i = 0 ; i < arr. length; i++ ) {
if ( result. indexOf ( arr[ i] ) == - 1 ) result. push ( arr[ i] ) ;
}
return result;
}
function _new ( func, ... args ) {
let obj = { } ;
obj. __proto__ = func. prototype;
let result = func . apply ( obj, args) ;
return typeof result === 'object' ? result : obj;
}
function Person ( name, age ) {
this . name = name;
this . age = age;
}
let obj = _new ( Person, 'xia' , 20 ) ;
console. log ( obj) ;
Ajax
function _ajax ( ) {
var ajax = new XMLHttpRequest ( ) ;
ajax. onreadystatechange = function ( ) {
if ( ajax. readyState == 4 && ajax. status == 200 ) {
console. log ( ajax. responseText) ;
}
} ;
ajax. open ( 'get' , 'getStar.php?starName=' + name) ;
ajax. send ( ) ;
}