1,?前面的字符大写
const str = 'i?? love??? ? word' ;
const handleStr = ( newstr ) => {
let str = newstr. split ( ' ' ) . join ( '' ) ;
let len = str. length;
let result = '' ;
for ( let index = 0 ; index < len; index++ ) {
const element = str[ index] ;
let newElement = str[ index] ;
if ( str[ index] !== '?' && str[ index] !== ' ' ) {
if ( str[ index - 1 ] === '?' ) {
newElement = ' ' + newElement. toUpperCase ( ) ;
}
result += newElement
}
}
return result;
}
handleStr ( str) ;
console. log ( handleStr ( str) ) ;
const str2 = 'i?? love??? ? word' ;
const handleStr2 = ( newstr ) => {
let result = '' ;
let isFirst = false ;
for ( const word of newstr) {
if ( word !== '?' && word !== ' ' ) {
result += isFirst ? ` ${ word. toUpperCase ( ) } ` : word;
isFirst = false ;
} else if ( word === '?' ) {
isFirst = true ;
}
}
return result;
}
console. log ( handleStr2 ( str2) ) ;
2,循环生成字符串
const getStr = function ( str ) {
let start = 0 ;
let end = 0 ;
let newStr = "" ;
for ( let index = 0 ; index < str. length; index++ ) {
const element = str[ index] ;
if ( element === "[" ) {
start = index;
} else {
if ( element === "]" ) {
end = index;
break ;
}
}
}
if ( start === 0 && end === 0 ) {
return str;
} else {
let dataStr = str. slice ( start + 1 , end) ;
let count = str[ start - 1 ] ;
let results = "" ;
for ( let j = 0 ; j < Number ( count) ; j++ ) {
results = results + dataStr;
}
newStr = str. replace ( str. slice ( start - 1 , end + 1 ) , results) ;
return getStr ( newStr) ;
}
} ;
console. log ( getStr ( "2[a]3[b]4[c]" ) ) ;
console. log ( getStr ( "2[ab]3[cd]" ) ) ;
console. log ( getStr ( "3[a2[cd]]" ) ) ;
3,原生实现vue2双向绑定
< ! 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> vue 双向绑定实现< / title>
< / head>
< body>
< div> yuzhi< / div>
< script>
const Observer = function ( data ) {
for ( let key in data) {
defineReactive ( data, key) ;
}
}
const defineReactive = function ( obj, key ) {
const dep = new Dep ( ) ;
debugger
let val = obj[ key] ;
Object. defineProperty ( obj, key, {
enumerable : true ,
configurable : true ,
get ( val) {
console. log ( 'in get' , val) ;
debugger
dep. depend ( ) ;
return val;
} ,
set ( newVal) {
if ( newVal === val) {
return ;
}
val = newVal;
dep. notify ( ) ;
}
} ) ;
}
const observe = function ( data ) {
return new Observer ( data) ;
}
const Vue = function ( options ) {
const self = this ;
if ( options && typeof options. data === 'function' ) {
this . _data = options. data . apply ( this ) ;
}
this . mount = function ( ) {
new Watcher ( self, self. render) ;
}
this . render = function ( ) {
with ( self) {
_data. text;
}
}
observe ( this . _data) ;
}
const Watcher = function ( vm, fn ) {
const self = this ;
this . vm = vm;
Dep. target = this ;
this . addDep = function ( dep ) {
dep. addSub ( self) ;
}
this . update = function ( ) {
console. log ( 'in watcher update' ) ;
fn ( ) ;
}
this . value = fn ( ) ;
Dep. target = null ;
}
const Dep = function ( ) {
const self = this ;
this . target = null ;
this . subs = [ ] ;
this . depend = function ( ) {
if ( Dep. target) {
Dep. target. addDep ( self) ;
}
}
this . addSub = function ( watcher ) {
self. subs. push ( watcher) ;
}
this . notify = function ( ) {
for ( let i = 0 ; i < self. subs. length; i += 1 ) {
self. subs[ i] . update ( ) ;
}
}
}
const vue = new Vue ( {
data ( ) {
return {
text : 'hello world'
} ;
}
} )
vue. mount ( ) ;
< / script>
< / body>
< / html>