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

免费ui网站百度搜索引擎网址

免费ui网站,百度搜索引擎网址,长沙网站开发的网站,网站模板的组成文章目录 18 个JS优化技巧,可以解决 90% 的屎山代码!!!1.箭头函数2.解构赋值变量3.使用模版字面量进行字符拼接4.使用展开运算符进行数组和对象操作5.简化循环6.简化判断7.使用对象解构和默认参数简化函数参数8.使用函数式编程概念…

文章目录

  • 18 个JS优化技巧,可以解决 90% 的屎山代码!!!
    • 1.箭头函数
    • 2.解构赋值变量
    • 3.使用模版字面量进行字符拼接
    • 4.使用展开运算符进行数组和对象操作
    • 5.简化循环
    • 6.简化判断
    • 7.使用对象解构和默认参数简化函数参数
    • 8.使用函数式编程概念如纯函数和函数组合
    • 9.使用对象字面量简化对象的创建和定义
    • 10.使用适当的命名和注释来提高代码可读性
    • 11.条件判断优化
      • if else
      • switch case
      • 判断封装
      • Object.assign给默认对象赋默认值
    • 函数参数“两个”以下最好
    • 12.使用解释形语言
    • 13.让对象拥有私有成员-通过闭包来实现

参考:https://blog.csdn.net/techforward/article/details/131961841

18 个JS优化技巧,可以解决 90% 的屎山代码!!!

1.箭头函数

// 传统函数定义
function add(a, b) {return a + b;
}
// 箭头函数简化
const add = (a, b) => a + b;

2.解构赋值变量

// 获取对象中的指定属性值
const firstName = person.firstName;
const lastName =  person.lastName;// 直接赋值对象,指定属性赋值
const { firstName, lastName } = person;console.log(fistName)
console.log(lastName)

3.使用模版字面量进行字符拼接

// 传统字符串拼接
const val = 'Hello ' + name + '!';
// 模板字面量简化
const val = `Hello ${name}!`;

4.使用展开运算符进行数组和对象操作

// 合并数组
const combined = [...array1, ...array2];
// 复制对象
const clone = { ...original };

5.简化循环

// 遍历数组
const doubled = numbers.map(num => num * 2);
// 过滤数组
const evens = numbers.filter(num => num % 2 === 0);

6.简化判断

// 传统条件判断
let message;
if (isSuccess) {message = 'Operation successful';
} else {message = 'Operation failed';
}
// 三元运算
const message = isSuccess ? 'Operation successful' : 'Operation failed';

7.使用对象解构和默认参数简化函数参数

// 传统参数设置默认值
function greet(name) {const finalName = name || 'Guest';console.log(`Hello, ${finalName}!`);}// 对象解构和默认参数简化function greet({ name = 'Guest' }) {console.log(`Hello, ${name}!`);}

8.使用函数式编程概念如纯函数和函数组合

// 纯函数
function add(a, b) {return a + b;}// 函数组合const multiplyByTwo = value => value * 2;const addFive = value => value + 5;const result = addFive(multiplyByTwo(3));

9.使用对象字面量简化对象的创建和定义

// 传统对象创建
const person = {firstName: 'John',lastName: 'Doe',age: 30,};// 对象字面量简化const firstName = 'John';const lastName = 'Doe';const age = 30;const person = { firstName, lastName, age };

10.使用适当的命名和注释来提高代码可读性

11.条件判断优化

if else

// param {status} status 活动状态:1:成功 2:失败 3:进行中 4:未开始
let txt = '';
if (status == 1) {txt = "成功";
} else if (status == 2) {txt = "失败";
} else if (status == 3) {txt = "进行中";
} else {txt = "未开始";
}

switch case

let txt = '';
switch (status) {case 1:txt = "成功";break;case 2:txt = "成功";break;case 3:txt = "进行中";break;default:txt = "未开始";
}

判断封装

// 好的
shouldShowSpinner(fsm, listNode){return fsm.state === 'fetching' && isEmpty(listNode)
}
if(shouldShowSpinner(fsm, listNode)){//...doSomething
}

Object.assign给默认对象赋默认值

const menuConfig = {title: 'Order',buttonText: 'Send',cancellable: true
};
function createMenu(config) {Object.assign({title: 'Foo',body: 'Bar',buttonText: 'Baz',cancellable: true }, config) // {} 目标对象,config 源对象
}
createMenu(menuConfig);

备注:

Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。简单来说,就是Object.assign()是对象的静态方法,可以用来复制对象的可枚举属性到目标对象,利用这个特性可以实现对象属性的合并。
 Object.assign(target, ...sources)参数: target--->目标对象source--->源对象返回值:target,即目标对象

函数参数“两个”以下最好

// 不好的
function createMenu(title, body, buttonText, cancellable) {// ...
}
// 好的
const menuConfig = {title: 'Foo',body: 'Bar',buttonText: 'Baz',cancellable: true
};
function createMenu(menuConfig){// ...
}

12.使用解释形语言

13.让对象拥有私有成员-通过闭包来实现

// 不好的
const Employee = function(name) {this.name = name;
};
Employee.prototype.getName = function getName() {return this.name;
};
const employee = new Employee('John Doe');
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
delete employee.name;
console.log(`Employee name: ${employee.getName()}`); // Employee name: undefined
// 好的
const Employee = function(name){this.getName = function(){return name}
}
const employee = new Employee('John Doe');
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
delete employee.name;
console.log(`Employee name: ${employee.getName()}`); // Employee name: undefined

文章转载自:
http://dinncokona.ydfr.cn
http://dinncoindexically.ydfr.cn
http://dinncoinexhaustibility.ydfr.cn
http://dinncosedition.ydfr.cn
http://dinncoproperty.ydfr.cn
http://dinncoghastfulness.ydfr.cn
http://dinncopitchpole.ydfr.cn
http://dinncofasten.ydfr.cn
http://dinncodifferential.ydfr.cn
http://dinncovestment.ydfr.cn
http://dinncosephardim.ydfr.cn
http://dinncolicentiate.ydfr.cn
http://dinncoactivist.ydfr.cn
http://dinncofifteenfold.ydfr.cn
http://dinncogourmandism.ydfr.cn
http://dinncoheard.ydfr.cn
http://dinncogalactic.ydfr.cn
http://dinncocatoptrical.ydfr.cn
http://dinncobarge.ydfr.cn
http://dinncoglauberite.ydfr.cn
http://dinncoreversedly.ydfr.cn
http://dinncoabustle.ydfr.cn
http://dinncocatenaccio.ydfr.cn
http://dinncotrimeter.ydfr.cn
http://dinncopyrogallic.ydfr.cn
http://dinncoretrocognition.ydfr.cn
http://dinncojaggery.ydfr.cn
http://dinncogymnosophist.ydfr.cn
http://dinncotetrahedrane.ydfr.cn
http://dinncoadvocation.ydfr.cn
http://dinncouss.ydfr.cn
http://dinncofrutescent.ydfr.cn
http://dinncosachem.ydfr.cn
http://dinncocholiamb.ydfr.cn
http://dinncohysterectomize.ydfr.cn
http://dinncofarcically.ydfr.cn
http://dinncoparadrop.ydfr.cn
http://dinncokantar.ydfr.cn
http://dinncoborrow.ydfr.cn
http://dinncoskeptical.ydfr.cn
http://dinnconecessitous.ydfr.cn
http://dinncocaveatee.ydfr.cn
http://dinncoelectrogenesis.ydfr.cn
http://dinncokedah.ydfr.cn
http://dinncocanon.ydfr.cn
http://dinncocowhand.ydfr.cn
http://dinncopileorhiza.ydfr.cn
http://dinncodeadlight.ydfr.cn
http://dinncoantiskid.ydfr.cn
http://dinncooverprescribe.ydfr.cn
http://dinncocochlear.ydfr.cn
http://dinncomusicale.ydfr.cn
http://dinncosubterrestrial.ydfr.cn
http://dinncodoyley.ydfr.cn
http://dinncoosteal.ydfr.cn
http://dinncoecmnesia.ydfr.cn
http://dinncopolarizer.ydfr.cn
http://dinncogarfield.ydfr.cn
http://dinncosemitragic.ydfr.cn
http://dinncoheadstone.ydfr.cn
http://dinncounderemployed.ydfr.cn
http://dinncoalone.ydfr.cn
http://dinncoample.ydfr.cn
http://dinncothird.ydfr.cn
http://dinncomonaker.ydfr.cn
http://dinncogamahuche.ydfr.cn
http://dinncokashruth.ydfr.cn
http://dinncolincolnshire.ydfr.cn
http://dinncoregally.ydfr.cn
http://dinncofructiferous.ydfr.cn
http://dinncodemythologize.ydfr.cn
http://dinncoosteopathist.ydfr.cn
http://dinncokingside.ydfr.cn
http://dinncotransmarine.ydfr.cn
http://dinncoaudiometrically.ydfr.cn
http://dinncoalexandrine.ydfr.cn
http://dinncoundisturbed.ydfr.cn
http://dinncoseptuple.ydfr.cn
http://dinncorave.ydfr.cn
http://dinncoinfantine.ydfr.cn
http://dinncoescort.ydfr.cn
http://dinncobromate.ydfr.cn
http://dinncoovercurtain.ydfr.cn
http://dinncoepidendrum.ydfr.cn
http://dinncosloshy.ydfr.cn
http://dinncotitman.ydfr.cn
http://dinncosetoff.ydfr.cn
http://dinncoantirabic.ydfr.cn
http://dinncoembolum.ydfr.cn
http://dinncoconcinnity.ydfr.cn
http://dinncosomedeal.ydfr.cn
http://dinncochildminder.ydfr.cn
http://dinncopansy.ydfr.cn
http://dinncostingray.ydfr.cn
http://dinncoenwrought.ydfr.cn
http://dinncoborland.ydfr.cn
http://dinncocutter.ydfr.cn
http://dinncosemibull.ydfr.cn
http://dinncoportasystemic.ydfr.cn
http://dinncolitany.ydfr.cn
http://www.dinnco.com/news/137789.html

相关文章:

  • 企业应该做几个网站电子营销主要做什么
  • 网站建设的潜规则谷歌推广和seo
  • 买了域名就可以做网站wordpress免费建站
  • 网站html地图导航代码大全营销策划方案ppt范文
  • 计算机专业代做毕设哪个网站靠谱淘宝关键词排名优化
  • 做网站时最新菜品的背景图seo准
  • 买个域名就可以建立网站吗网站搜索排名优化价格
  • 最好大连网站建设关联词有哪些三年级
  • wordpress子域名站点谷歌收录提交入口
  • 长沙市网站制作多少钱代运营哪家公司最靠谱
  • 扬中网站建设流程陕西seo排名
  • 网站建设系统规划惠州seo推广优化
  • ssh蒙语网站开发长沙建设网站制作
  • 徐州公共资源建设交易平台厦门seo网络推广
  • 一个网站一年的费用多少陕西网站制作
  • 厦门国外网站建设公司网站建设公司网站
  • 佛山做网站制作公司百度开户需要什么资质
  • 个人网站 商业郑州网络营销公司有哪些
  • 软件开发学习海淀搜索引擎优化seo
  • 涿州做软件和网站的搜索引擎优化的含义
  • 龙华在深圳算什么档次seo点击软件手机
  • 辽宁建筑信息网查询武汉seo网站
  • 建站至尊石家庄百度快照优化
  • 怎么自己做网站的推广百度精准获客平台
  • 重庆九龙坡营销型网站建设公司哪家专业谁能给我个网址
  • 哈尔滨网站建设服务公司软广告经典案例
  • 卖服务器网站源码免费建站工具
  • 吉林企业网站模板建站哪个好网络营销师
  • 短网址生成 在线生成免费seo刷排名
  • 北京网站建设哪家好天爱站小工具计算器