三元表达式之理解/jquery源码分析之$.inArray实现

解析jQuery源码中的三元表达式断句技巧
本文详细解析了jQuery源码中三元表达式的读法及断句方法,通过实例展示了如何正确理解和应用三元表达式,包括缓存arr.length的用法及inArray实现过程。

每次看到三元表达式就会惶惶然分不清如何读,正如语文中的断句一样,jquery源码中的三元表达式更是不知如何断句。

附jquery中的inArray实现。


大家熟悉jquery的应该都不陌生inArray的用法。在源码实现中,先判断了arr是否存在,如果不存在,则可直接返回-1,否则进行后续判断,当存在indexOf方法时,则直接使用call来调用indexOf方法。

否则继续。

在jquery代码中处处体现代码构建者对于javascript的深刻理解,譬如这里对arr.length的缓存,接下来进入我们的重头戏。

下面那句到处应该怎么理解。

是i=i?这样断么。好像不太对哦。完全理解不通。其实不然,

这是群里朋友给予我的三元表达式的断句方法,所以在这里的应用就是:

给 i 赋值后面自i?i<0?开始的整个句子。后面的判断应该是若i不存在,则给 i 赋值于0,即是从数组开头处找起,否则判断存在的i是否小于0,若是,则给i赋值为Math.max(0,length+i),否则赋值为i。

后续得到的i则是我们需要从数组开始查找元素elem的位置。

 也就是这个inArray的用法。结束啦。。。。代码界的小菜小菜

<!-- 客户信息新增/Add --> <!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>Document</title> <link rel="stylesheet" href="../../layui/css/layui.css"> <link rel="stylesheet" href="../../css/diy.css"> <script src="../../js/axios.min.js"></script> <style> img { width: 200px; } .layui-upload-list{ overflow: hidden; } .layui-upload-list .multiple_block .upload_img_multiple{ height: auto; width: 100px; } .multiple_block{ position: relative; float: left; width: 100px; margin: 0 10px 10px 0; } .multiple_block .upload-img-del{ position: absolute; top: 5px; right: 5px; color: #fff; border-radius: 100%; background: #0000009c; width: 20px; height: 20px; text-align: center; line-height: 20px; cursor: pointer; } </style> </head> <body> <article class="sign_in"> <div class="warp tpl"> <div class="layui-container"> <div class="layui-row"> <form class="layui-form" action=""> <div class="form-input-box from-input-box-i"> <div class="layui-form-item unique" id="client_company_box"> <label class="layui-form-label">客户公司</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入客户公司" class="layui-input" id="client_company"> </div> </div> <div class="layui-form-item unique" id="customer_name_box"> <label class="layui-form-label">客户姓名</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入客户姓名" class="layui-input" id="customer_name"> </div> </div> <div class="layui-form-item select-box" id="customer_gender_box"> <label class="layui-form-label">客户性别</label> <div class="layui-input-block select block"> <select name="interest" lay-filter="customer_gender" id="customer_gender"> <option value=""></option> </select> </div> </div> <div class="layui-form-item" id="customer_phone_number_box"> <label class="layui-form-label">客户电话</label> <div class="layui-input-block input-i block"> <input type="text" name="title" lay-verify="phone_number" autocomplete="off" placeholder="请输入客户电话" class="layui-input" id="customer_phone_number"> </div> </div> <div class="layui-form-item layui-form-text" id="customer_address_box"> <label class="layui-form-label">客户地址</label> <div class="layui-input-block text"> <textarea placeholder="请输入客户地址" class="layui-textarea" id="customer_address"></textarea> </div> </div> <div class="layui-form-item layui-form-text" id="customer_preferences_box"> <label class="layui-form-label">客户偏好</label> <div class="layui-input-block text"> <textarea placeholder="请输入客户偏好" class="layui-textarea" id="customer_preferences"></textarea> </div> </div> </div> </form> <div class="layui-btn-container"> <button type="button" class="layui-btn layui-btn-normal login" id="submit">确认</button> <button type="button" class="layui-btn layui-btn-normal login" id="cancel">取消</button> </div> </div> </div> </div> </article> </body> <script src="../../layui/layui.js"></script> <script src="../../js/base.js"></script> <script src="../../js/index.js"></script> <script> var BaseUrl = baseUrl() let cancel = document.querySelector("#cancel") cancel.addEventListener("click",()=>{ colseLayer() }) let customer_information_id = location.search.substring(1) layui.use(['upload', 'element', 'layer', 'laydate', 'layedit'], function () { var $ = layui.jquery , upload = layui.upload , element = layui.element , layer = layui.layer , laydate = layui.laydate , layedit = layui.layedit , form = layui.form; let url let token = sessionStorage.token || null let personInfo = JSON.parse(sessionStorage.personInfo) let user_group = personInfo.user_group let use_id = personInfo.user_id function $get_stamp() { return new Date().getTime(); } function $get_rand(len) { var rand = Math.random(); return Math.ceil(rand * 10 ** len); } // 权限判断 /** * 获取路径对应操作权限 鉴权 * @param {String} action 操作名 */ function $check_action(path1, action = "get") { var o = $get_power(path1); if (o && o[action] != 0 && o[action] != false) { return true; } return false; } /** * 是否有显示或操作字段的权限 * @param {String} action 操作名 * @param {String} field 查询的字段 */ function $check_field(action, field, path1) { var o = $get_power(path1); var auth; if (o && o[action] != 0 && o[action] != false) { auth = o["field_" + action]; } if (auth) { return auth.indexOf(field) !== -1; } return false; } /** * 获取权限 * @param {String} path 路由路径 */ function $get_power(path) { var list_data = JSON.parse(sessionStorage.list_data) var list = list_data; var obj; for (var i = 0; i < list.length; i++) { var o = list[i]; if (o.path === path) { obj = o; break; } } return obj; } let submit = document.querySelector('#submit') // 提交按钮校验权限 if ($check_action('/customer_information/view', 'add') || $check_action('/customer_information/view', 'set') || $check_option('/customer_information/table', 'examine')) { }else { $("#submit").hide() } // style="display: none" let field = "customer_information_id"; let url_add = "customer_information"; let url_set = "customer_information"; let url_get_obj = "customer_information"; let url_upload = "customer_information" let query = { "customer_information_id": 0, } let form_data2 = { "client_company": '', // 客户公司 "customer_name": '', // 客户姓名 "customer_gender": '', // 客户性别 "customer_phone_number": '', // 客户电话 "customer_address": '', // 客户地址 "customer_preferences": '', // 客户偏好 "customer_information_id": 0, // ID } layui.layedit.set({ uploadImage: { url: BaseUrl + '/api/customer_information/upload?' //接口url , type: 'post' //默认post } }); var path1 function getpath() { var list_data = JSON.parse(sessionStorage.list_data) for (var i = 0; i < list_data.length; i++) { var o = list_data[i]; if (o.path === "/customer_information/table") { path1 = o.path $get_power(o.path) } } } getpath() /** * 注册时是否有显示或操作字段的权限 * @param {String} action 操作名 * @param {String} field 查询的字段 * @param {String} path 路径 */ function $check_register_field(action, field, path1) { var o = $get_power(path1); var auth; if (o && o[action] != 0 && o[action] != false) { auth = o["field_" + action]; } if (auth) { return auth.indexOf(field) !== -1; } return false; } /** * 是否有显示或操作字段的权限 * @param {String} action 操作名 * @param {String} field 查询的字段 */ function $check_field(action, field) { var o = $get_power("/customer_information/view"); var auth; if (o && o[action] != 0 && o[action] != false) { auth = o["field_" + action]; } if (auth) { return auth.indexOf(field) !== -1; } return false; } /** * 获取路径对应操作权限 鉴权 * @param {String} action 操作名 */ function $check_exam(path1, action = "get") { var o = $get_power(path1); if (o) { var option = JSON.parse(o.option); if (option[action]) return true } return false; } function $check_option(path,op) { var o = $get_power(path); if (o){ var option = JSON.parse(o.option); if (option[op]) return true } return false; } /** * 是否有审核字段的权限 */ function $check_examine() { var url = window.location.href; var url_ = url.split("/") var pg_url = url_[url_.length - 2] let path = "/"+ pg_url + "/table" var o = $get_power(path); if (o){ var option = JSON.parse(o.option); if (option.examine) return true } return false; } if ($check_field('add', 'client_company')){ $("#client_company_box").show() }else { $("#client_company_box").hide() } if ($check_field('add', 'customer_name')){ $("#customer_name_box").show() }else { $("#customer_name_box").hide() } if ($check_field('add', 'customer_gender')){ $("#customer_gender_box").show() }else { $("#customer_gender_box").hide() } if ($check_field('add', 'customer_phone_number')){ $("#customer_phone_number_box").show() }else { $("#customer_phone_number_box").hide() } if ($check_field('add', 'customer_address')){ $("#customer_address_box").show() }else { $("#customer_address_box").hide() } if ($check_field('add', 'customer_preferences')){ $("#customer_preferences_box").show() }else { $("#customer_preferences_box").hide() } // 客户性别选项列表 let customer_gender_data = ['男','女']; async function customer_gender() { var customer_gender = document.querySelector("#customer_gender") var op1 = document.createElement("option"); op1.value = '0' customer_gender.appendChild(op1) // 收集数据 长度 var count // 收集数据 数组 var arr = [] count = customer_gender_data.length arr = customer_gender_data for (var i = 0; i < arr.length; i++) { var op = document.createElement("option"); // 给节点赋值 op.innerHTML = arr[i] op.value = arr[i] // 新增/Add节点 customer_gender.appendChild(op) if (form_data2.customer_gender==arr[i].customer_gender){ op.selected = true } layui.form.render("select"); } } layui.form.on('select(customer_gender)', function (data) { form_data2.customer_gender = data.elem[data.elem.selectedIndex].text; }) customer_gender() //文本 let client_company = document.querySelector("#client_company") client_company.onkeyup = function (event) { form_data2.client_company = event.target.value } //文本 //文本 let customer_name = document.querySelector("#customer_name") customer_name.onkeyup = function (event) { form_data2.customer_name = event.target.value } //文本 //多文本 let customer_address = document.querySelector("#customer_address") //多文本 //多文本 let customer_preferences = document.querySelector("#customer_preferences") //多文本 var data = sessionStorage.data || '' if (data !== '') { var data2 = JSON.parse(data) Object.keys(form_data2).forEach(key => { Object.keys(data2).forEach(dbKey => { if (key === dbKey) { if (key!=='examine_state' && key!=='examine_reply'){ $('#' + key).val(data2[key]) form_data2[key] = data2[key] $('#' + key).attr('disabled', 'disabled') for (let key in form_data2) { if (key == 'customer_gender') { let alls = document.querySelector('#customer_gender').querySelectorAll('option') let test = form_data2[key] for (let i = 0; i < alls.length; i++) { if (alls[i].innerHTML == test) { alls[i].selected = true form_data2.customer_gender = alls[i].text layui.form.render("select"); } } } } } } if(dbKey === "source_table"){ form_data2.source_table = data2[dbKey]; } if(dbKey === "source_id"){ form_data2.source_id = data2[dbKey]; } if(dbKey === "source_user_id"){ form_data2.source_user_id = data2[dbKey]; } }) }) sessionStorage.removeItem("data"); } if (customer_information_id !== '') { $('#print').show(); async function axios_get_3() { const {data: rese} = await axios.get(BaseUrl + '/api/customer_information/get_obj', { params: { customer_information_id: customer_information_id }, headers: { 'x-auth-token': token } }) let data = rese.result.obj Object.keys(form_data2).forEach((key) => { form_data2[key] = data[key]; $("#"+key).val(form_data2[key]) }); for (let key in data) { if (key == 'customer_gender') { let alls = document.querySelector('#customer_gender').querySelectorAll('option') let test = data[key] for (let i = 0; i < alls.length; i++) { if (alls[i].innerHTML == test) { alls[i].selected = true form_data2.customer_gender = alls[i].text layui.form.render("select"); } } } } if ($check_field('set', 'client_company') || $check_field('get', 'client_company')){ $("#client_company_box").show() }else { $("#client_company_box").hide() } if ($check_field('set', 'customer_name') || $check_field('get', 'customer_name')){ $("#customer_name_box").show() }else { $("#customer_name_box").hide() } if ($check_field('set', 'customer_gender') || $check_field('get', 'customer_gender')){ $("#customer_gender_box").show() }else { $("#customer_gender_box").hide() } if ($check_field('set', 'customer_phone_number') || $check_field('get', 'customer_phone_number')){ $("#customer_phone_number_box").show() }else { $("#customer_phone_number_box").hide() } if ($check_field('set', 'customer_address') || $check_field('get', 'customer_address')){ $("#customer_address_box").show() }else { $("#customer_address_box").hide() } if ($check_field('set', 'customer_preferences') || $check_field('get', 'customer_preferences')){ $("#customer_preferences_box").show() }else { $("#customer_preferences_box").hide() } // Array.prototype.slice.call(document.getElementsByTagName('input')).map(i => i.disabled = true) // Array.prototype.slice.call(document.getElementsByTagName('select')).map(i => i.disabled = true) // Array.prototype.slice.call(document.getElementsByTagName('textarea')).map(i => i.disabled = true) //文本 client_company.value = form_data2.client_company //文本 if ((form_data2['customer_information_id'] && $check_field('set', 'client_company')) || (!form_data2['customer_information_id'] && $check_field('add', 'client_company'))) { }else { $("#client_company").attr("disabled", true); $("#client_company > input[name='file']").attr('disabled', true); } //文本 customer_name.value = form_data2.customer_name //文本 if ((form_data2['customer_information_id'] && $check_field('set', 'customer_name')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_name'))) { }else { $("#customer_name").attr("disabled", true); $("#customer_name > input[name='file']").attr('disabled', true); } if ((form_data2['customer_information_id'] && $check_field('set', 'customer_gender')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_gender'))) { }else { $("#customer_gender").attr("disabled", true); $("#customer_gender > input[name='file']").attr('disabled', true); } if ((form_data2['customer_information_id'] && $check_field('set', 'customer_phone_number')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_phone_number'))) { }else { $("#customer_phone_number").attr("disabled", true); $("#customer_phone_number > input[name='file']").attr('disabled', true); } //多文本 customer_address.value = form_data2.customer_address //多文本 if ((form_data2['customer_information_id'] && $check_field('set', 'customer_address')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_address'))) { }else { $("#customer_address").attr("disabled", true); $("#customer_address > input[name='file']").attr('disabled', true); } //多文本 customer_preferences.value = form_data2.customer_preferences //多文本 if ((form_data2['customer_information_id'] && $check_field('set', 'customer_preferences')) || (!form_data2['customer_information_id'] && $check_field('add', 'customer_preferences'))) { }else { $("#customer_preferences").attr("disabled", true); $("#customer_preferences > input[name='file']").attr('disabled', true); } layui.form.render("select"); } axios_get_3() } submit.onclick = async function () { try { //文本 form_data2.client_company = client_company.value //文本 //文本 form_data2.customer_name = customer_name.value //文本 //文本 form_data2.customer_phone_number = customer_phone_number.value //文本 //多文本 form_data2.customer_address = customer_address.value //多文本 //多文本 form_data2.customer_preferences = customer_preferences.value //多文本 } catch (err) { console.log(err) } let customer_phone_number_phone_regular = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; if(form_data2.customer_phone_number && !customer_phone_number_phone_regular.test(form_data2.customer_phone_number)){ layer.msg('手机号格式错误'); return; } let customize_field = [] customize_field.push({"field_name": "客户公司", "field_value": form_data2.client_company}); customize_field.push({"field_name": "客户姓名", "field_value": form_data2.customer_name}); customize_field.push({"field_name": "客户性别", "field_value": form_data2.customer_gender}); customize_field.push({"field_name": "客户电话", "field_value": form_data2.customer_phone_number}); customize_field.push({"field_name": "客户地址", "field_value": form_data2.customer_address}); customize_field.push({"field_name": "客户偏好", "field_value": form_data2.customer_preferences}); if (customer_information_id == '') { console.log("新增/Add") const {data: res} = await axios.post(BaseUrl + '/api/customer_information/add?', form_data2, { headers: { 'x-auth-token': token, 'Content-Type': 'application/json' } }) if (res.result == 1) { layer.msg('确认完毕'); setTimeout(function () { colseLayer() }, 1000) }else { layer.msg(res.error.message); } } else { console.log("详情/Details") const {data: res} = await axios.post(BaseUrl + '/api/customer_information/set?customer_information_id=' + customer_information_id, form_data2, { headers: { 'x-auth-token': token, 'Content-Type': 'application/json' } }) if (res.result == 1) { layer.msg('确认完毕'); setTimeout(function () { colseLayer() }, 1000) }else { layer.msg(res.error.message); } } } }) ; </script> </html> 逐个分析我给的代码内容,代码对应的功能,不要自己写
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值