av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - js如何實(shí)現(xiàn)這種操作,get(obj,’k1’,’k2’,’k3’).then((v)=>console.log(v))

瀏覽:120日期:2023-06-01 17:34:32

問題描述

類似下面這種代碼

get(obj,’k1’,’k2’,’k3’) .then((v)=>console.log(v)) .else(()=>console.log(’值為空’));

實(shí)現(xiàn)下面這種代碼的功能

if(obj && obj.k1 && obj.k1.k2 && obj.k1.k2.k3){ console.log(obj.k1.k2.k3);}else{ console.log(’值為空’)}

問題解答

回答1:

采用es6+promise實(shí)現(xiàn)方式

// 功能實(shí)現(xiàn)function get(obj, ...props) { // 檢查該對象是否擁有某個屬性 function hasProp(obj, prop) { return !!obj[prop] } return new Promise(function(resolve, reject) { let tempObj = {...obj} for (let i = 0; i < props.length; i++) { // 如果找到該屬性,將該屬性存儲起來繼續(xù)尋找下一個屬性,直到循環(huán)結(jié)束 if (hasProp(tempObj, props[i])) {tempObj = tempObj[props[i]] } else { // 找不到則返回錯誤信息return reject(’找不到’ + props[i] + ’屬性’) } } return resolve(tempObj) })}// 使用let obj = { user: { name: ’anguer’ }}get(obj, ’user’, ’name’).then(function(res) { console.log(res) // print ’anguer’}).catch(function(err) { console.log(err)})回答2:

這樣行不行

function get (obj) { var scope = { obj: obj } var path = ’scope.obj.’ + Array.prototype.slice.call(arguments, 1).join(’.’) var value = null var NONE = ’值為空’ try {value = (new Function(’scope’, ’return ’ + path + ’;’))(scope)if (value === null || value === undefined) { return NONE } else { return value} } catch (e) {return NONE }}var obj = { k1: { k2: { k3: 1}}}get(obj, ’k1’, ’k2’, ’k3’) // 1get(obj, ’k1’, ’k’, ’k3’) // 值為空回答3:

class Tang { constructor() { this.obj = null; this.keys = []; this.thenF = []; this.elseF = []; } then(fn) { this.thenF.push(fn); return this; } _init() { let [obj, ...keys] = arguments; this.obj = obj; this.keys = keys; setTimeout(() => this._start(), 0) return this; } _start() { while(this.keys.length && this.obj) { this.obj = this.obj[this.keys.shift()]; } if (!this.keys.length) { this.thenF.forEach(fn => fn(this.obj)); } else { this.elseF.forEach(fn => fn()); } } else(fn) { this.elseF.push(fn); return this; }}let obj = {k1:{k2:{k3:1}}};let tang = new Tang();let get = tang._init.bind(tang);get(obj,’k1’,’k2’,’k3’) .then((v)=>console.log(v)) .else(()=>console.log(’值為空’));

看到鏈?zhǔn)秸{(diào)用我就想到了之前的lazyman。實(shí)現(xiàn)的比較丑陋。。。

回答4:

參考一下

function get (obj, ...keys) { try { let value = keys.reduce((o, k) => o[k], obj) return { then (cb) {if (typeof cb === ’function’) { cb(value) }return {else () {}} } } } catch (e) { return { then () {return { else (cb) { if (typeof cb === ’function’) { cb(e) } }} } } }}

標(biāo)簽: JavaScript
主站蜘蛛池模板: 国产高清免费 | 国产一区不卡 | 国产精品一区二区久久久久 | 欧美成人h版在线观看 | 国产精品视频一区二区三区, | 日韩精品视频中文字幕 | 成人高潮片免费视频欧美 | 四虎影院免费在线播放 | 日本三级线观看 视频 | 五月激情久久 | 在线视频a | 日韩免费 | 精品国产乱码久久久久久牛牛 | 国产一二区免费视频 | 国内精品视频一区二区三区 | 殴美成人在线视频 | 色婷婷久久久久swag精品 | 97国产精品视频 | 亚洲欧洲一区二区 | 男人的天堂亚洲 | 国产高清在线观看 | 日本久久久久久 | 亚洲狠狠丁香婷婷综合久久久 | www.久久 | 国产日韩一区二区三免费高清 | 久久伊 | 天天插天天狠天天透 | 日日日干干干 | 超碰3| 黄免费观看视频 | 国产yw851.c免费观看网站 | 麻豆久久久久久 | 国产在线精品一区二区三区 | 这里只有精品999 | 狠狠入ady亚洲精品经典电影 | 亚洲欧美国产精品久久 | 亚洲精品在线免费观看视频 | 日韩欧美专区 | 久久久精品视频免费看 | 国产精品久久久久久 | 久久精品高清视频 |