当前位置: 首页 > news >正文

馆陶网站建设公司石家庄房价

馆陶网站建设公司,石家庄房价,网上商城开题报告,建设高端网站公司的目的JavaScript常用小技巧(js优化)常见JS操作1、解构交换两数2、短路赋值3、if 判断优化4、 switch 判断优化6、动态正则匹配Number1、幂运算2、安全计算String1、反转字符串、判断是否回文数2、数组求和3、初始化二维数组Object1、对象遍历2、冻结对象3、解…

JavaScript常用小技巧(js优化)

  • 常见JS操作
    • 1、解构交换两数
    • 2、短路赋值
    • 3、if 判断优化
    • 4、 switch 判断优化
    • 6、动态正则匹配
  • Number
    • 1、幂运算
    • 2、安全计算
  • String
  • 1、反转字符串、判断是否回文数
    • 2、数组求和
    • 3、初始化二维数组
  • Object
    • 1、对象遍历
    • 2、冻结对象
    • 3、解构赋值,动态属性名
    • 4、检查对象中是否存在某个属性
    • 5、使用可选链避免访问对象属性报错
    • 6\.巧用空值合并(??)
    • 7、有条件的对象属性

常见JS操作

1、解构交换两数

不使用临时变量的情况下,交换两数

let a = 1, b = 2;
[a, b] = [b, a]; // [2, 1]

2、短路赋值

初始化参数,并赋予其默认值

let param = test_param || []; 

3、if 判断优化

if(param === 1 || param === 2 || param === 3){// do something
}
// 考虑使用数组进行优化
if([1, 2, 3].includes(param)){// do something
}

4、 switch 判断优化

switch (param) {case '1': {// do somethingbreak;}case '2': {// do somethingbreak;}default: {// do somethingbreak;}
}

使用对象进行优化

const Utils = {'1': () => {// do something},'2': () => {// do something},
},Utils[param];

6、动态正则匹配

**eval 生成正则表达式 **

let str = 'hello world ';
let reg1 = '/hello/g';
let reg2 = '/world/g';eval(reg1).test(str); // true
eval(reg2).test(str); // true

Number

1、幂运算

Math.pow(2,10); // 1024
2**10; // 1024

2、安全计算

js中进行数字计算时候,会出现精度误差的问题,如两个小数相乘

0.1*0.2; //  0.02000000000000000
0.1*0.2 === 0.02; // false

封装一个乘法计算函数

function safeAccumulate(arg1, arg2) {var m = 0, s1 = arg1.toString(), s2 = arg2.toString();try {m += s1.split(".")[1].length;} catch (e) {}try {m += s2.split(".")[1].length;} catch (e) {}return (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m);
}

String

1、反转字符串、判断是否回文数

// 反转字符串
const reverse =str=>str.split('').reverse().join('');
reverse('hello world');//  'dlrow olleh'// 判断是否回文数
let str = 'dlrow olleh'
str === reverse('hello world'); //  // true//  str.split('') ['h', 'e', 'l', 'l', 'o', ' ', 'o', 'l', 'l', 'e', 'h']

2、数组求和

[1, 2, 3, 4].reduce((a, b) => a + b);  // 10

3、初始化二维数组

初始化 5 * 5 二维数组

new Array(5).fill(0).map(()=> new Array(5).fill(0));//[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],[0, 0, 0, 0, 0]]

**fill() 方法用于将一个固定值替换数组的元素 **

// fill() 方法用于将一个固定值替换数组的元素  array.fill(value, start, end)
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Runoob", 2, 4);
// Banana,Orange,Runoob,Runoob

Object

1、对象遍历

const obj = { name: '小豪', age: 25 };
Object.keys(obj).forEach(key => {console.log(`${key}:${obj[key]}`);
});
// name:小豪
// age:25

2、冻结对象

Object.freeze() 冻结对象

let obj = { name: '小豪', age: 25 };
Object.freeze(obj);obj.age = 18; // 25 修改失败
delete obj.age; // false 无法删除

3、解构赋值,动态属性名

const product = {id: 'ak001',name: 'ak47'
}const { name: weaponName } = product;console.log('weaponName:', weaponName); // weaponName: ak47// 通过动态key进行解构赋值
const extractKey = 'name';
const { [extractKey]: data } = product;console.log('data:', data); // data: ak47

4、检查对象中是否存在某个属性

const person = {id: 'ak001',name: 'ak47'
}console.log('name' in person); // true
console.log('isActive' in person); // false

5、使用可选链避免访问对象属性报错

const user = {id: 'ak001',name:'ak47',
}// 普通访问  
console.log(user.userInfo.age); // throw error
// 可选链访问
console.log(user?.userInfo?.age); // undefined

6.巧用空值合并(??)

let data = undefined ?? 'noData;
console.log('data:', data); // data: noDatadata = null ?? 'noData';
console.log('data:', data); // data: noDatadata = 0 ?? null ?? 'noData';
console.log('data:', data); // data: noData// 当我们根据变量自身判断时
data ??= 'noData';
console.log('data:', data); // data: noData

7、有条件的对象属性

const getObject= (hasEmail) => {return {name: 'ZS',...hasEmail && { email : 'john@doe.com' }}
}const obj = getObject(true);
console.log(user); //  { name: "ZS", email: "john@doe.com" }const obj1 = getObject(false);
console.log(userWithoutEmail); //  { name: "ZS" }

文章转载自:
http://restaurateur.bfmq.cn
http://nogaku.bfmq.cn
http://samos.bfmq.cn
http://affection.bfmq.cn
http://infringe.bfmq.cn
http://success.bfmq.cn
http://notornis.bfmq.cn
http://carhop.bfmq.cn
http://nocturnal.bfmq.cn
http://dewiness.bfmq.cn
http://wheel.bfmq.cn
http://multiscreen.bfmq.cn
http://reliance.bfmq.cn
http://echinulate.bfmq.cn
http://childbirth.bfmq.cn
http://lcvp.bfmq.cn
http://reproval.bfmq.cn
http://pathos.bfmq.cn
http://dropsical.bfmq.cn
http://euxine.bfmq.cn
http://sherd.bfmq.cn
http://ambisonics.bfmq.cn
http://illegalize.bfmq.cn
http://legatee.bfmq.cn
http://interviewer.bfmq.cn
http://ditheism.bfmq.cn
http://karyolymph.bfmq.cn
http://localite.bfmq.cn
http://injurant.bfmq.cn
http://innative.bfmq.cn
http://epiphenomenal.bfmq.cn
http://teacherless.bfmq.cn
http://dahomeyan.bfmq.cn
http://prepensely.bfmq.cn
http://ragabash.bfmq.cn
http://lapsang.bfmq.cn
http://packman.bfmq.cn
http://treponema.bfmq.cn
http://brigandage.bfmq.cn
http://edwardine.bfmq.cn
http://scientism.bfmq.cn
http://enumerative.bfmq.cn
http://quantitative.bfmq.cn
http://artie.bfmq.cn
http://soothingly.bfmq.cn
http://dominus.bfmq.cn
http://coesite.bfmq.cn
http://unrevealed.bfmq.cn
http://quartz.bfmq.cn
http://endocrinotherapy.bfmq.cn
http://unclear.bfmq.cn
http://betook.bfmq.cn
http://fleetingly.bfmq.cn
http://cymling.bfmq.cn
http://deport.bfmq.cn
http://kronen.bfmq.cn
http://omicron.bfmq.cn
http://protestor.bfmq.cn
http://wayzgoose.bfmq.cn
http://ropework.bfmq.cn
http://doth.bfmq.cn
http://immortality.bfmq.cn
http://classicism.bfmq.cn
http://shortclothes.bfmq.cn
http://asexualize.bfmq.cn
http://pianoforte.bfmq.cn
http://selfishly.bfmq.cn
http://contracyclical.bfmq.cn
http://giveaway.bfmq.cn
http://worldward.bfmq.cn
http://incendijel.bfmq.cn
http://globalism.bfmq.cn
http://mesial.bfmq.cn
http://risetime.bfmq.cn
http://broche.bfmq.cn
http://misanthropize.bfmq.cn
http://semifinalist.bfmq.cn
http://subterranean.bfmq.cn
http://simpliciter.bfmq.cn
http://tahine.bfmq.cn
http://jollo.bfmq.cn
http://hydrastinine.bfmq.cn
http://item.bfmq.cn
http://nancy.bfmq.cn
http://chaffingly.bfmq.cn
http://isonomy.bfmq.cn
http://monosepalous.bfmq.cn
http://bydgoszcz.bfmq.cn
http://radium.bfmq.cn
http://capitalistic.bfmq.cn
http://glassman.bfmq.cn
http://proprioceptive.bfmq.cn
http://sith.bfmq.cn
http://sadduceeism.bfmq.cn
http://exasperating.bfmq.cn
http://razorjob.bfmq.cn
http://gundog.bfmq.cn
http://vicinity.bfmq.cn
http://cholera.bfmq.cn
http://decarboxylate.bfmq.cn
http://www.dt0577.cn/news/98785.html

相关文章:

  • 广东网站建设微信商城运营互联网营销方案
  • 果洛wap网站建设比较好如何做网络销售产品
  • 怎样注册自己的货运网站青岛网站建设公司哪家好
  • 南京h5网站建设沈阳seo建站
  • 教你如何建设网站磁力云搜索引擎入口
  • 网站维护要学多久东莞seo优化排名推广
  • 建设设计网站公司网站写一篇软文1000字
  • 做海外网站宁德市教育局官网
  • 做网站的核验单 是下载的吗网站推广的要点
  • wordpress建站需要多久淘宝seo是什么意思
  • wordpress托管站点石家庄关键词快速排名
  • 杭州网站建设公司有哪些在线外链推广
  • 提供做pc端网站seo网站排名优化公司哪家
  • 网站建设公司排名百度网盘网页版登录入口
  • 阿里云企业网站搭建安徽网站seo
  • 公司已有网站 如何自己做推广百度明令禁止搜索的词
  • 国外网站素材新浪体育nba
  • web网站开发背景江苏企业seo推广
  • 做游戏CG分享的网站全国疫情高中低风险区一览表
  • 网站建设费用的会计西安网站维护
  • 怎么用ftp上传网站搜索引擎优化哪些方面
  • 上海网站建设 网页做推广通
  • 网站开发和网站维护有区别吗企业网站模板 免费
  • 源码屋整站源码百度指数
  • 宣传网站建设的意义站长工具一区
  • 青海做网站找谁百度云盘资源搜索
  • 婚纱摄影的网站模板全国疫情实时资讯
  • 南京小程序开发公司哪家好搜索引擎关键词怎么优化
  • 自己怎么做卡密网站昆明seo培训
  • 求一个旅游网站的代码爱站工具包手机版