函数实现 a?.b?.c?.d


function isHas(obj, key, str) {
const keyArr = obj.split('.');
  let i = 0;
  const is = (isObj, isKey) =>
     isObj ? is(isObj[isKey], keyArr[++i]) : str;
  const res = is(obj, keyArr[i]);
  is = null;keyArr = null;i = null;obj = null;key = null;str = null;
  return res;
}

var myObj = { a: 0 }

isHas(myObj, 'a.b.s') // undefined

isHas(myObj, 'a') // 0

/** 获取对象内指定属性的值 */ export function getObjVal(obj: Object, key: string, index: number = 0) {     const tkey = key.replace(/\[|(\].)/g, '.');     const keyArr = tkey.split('.');     const nIndex = index + 1;     if (keyArr.length < nIndex + 1) {         return obj;     }     if (!obj) {         return undefined;     }
    return getObjVal(obj[keyArr[nIndex]], key, nIndex); }  

var myObj1 = { a: 0, b: { ba: { baa: ['text'] } } };

getObjVal(myObj1, 'myObj1.b.ba.baa[0]') // text

getObjVal(myObj1, 'b.ba.baa[0]', 1) // text

getObjVal(myObj1, 'myObj1.a') // 0

getObjVal(myObj1, 'myObj1.a.a') // undefined