1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      JavaScript的簡寫寫法有哪些

      JavaScript的簡寫寫法有哪些,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

      創(chuàng)新互聯(lián)建站為您提適合企業(yè)的網(wǎng)站設(shè)計?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強的網(wǎng)絡(luò)競爭力!結(jié)合企業(yè)自身,進(jìn)行網(wǎng)站設(shè)計及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到做網(wǎng)站、成都網(wǎng)站制作, 我們的網(wǎng)頁設(shè)計師為您提供的解決方案。

      1.三元操作符

      當(dāng)想寫if...else語句時,使用三元操作符來代替。

      普通寫法:

      const x = 20;
      let answer;
      if (x > 10) {
       answer = 'is greater';
      } else {
       answer = 'is lesser';
      }

      簡寫:

      const answer = x > 10 ? 'is greater' : 'is lesser';

      也可以嵌套if語句:

      const big = x > 10 ? " greater 10" : x

      2.短路求值簡寫方式

      當(dāng)給一個變量分配另一個值時,想確定源始值不是nullundefined或空值。可以寫撰寫一個多重條件的if語句。

      if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
       let variable2 = variable1;
      }

      或者可以使用短路求值方法:

      const variable2 = variable1 || 'new';

      3.聲明變量簡寫方法

      let x;
      let y;
      let z = 3;

      簡寫方法:

      let x, y, z=3;

      4.if存在條件簡寫方法

      if (likeJavaScript === true)

      簡寫:

      if (likeJavaScript)

      只有l(wèi)ikeJavaScript是真值時,二者語句才相等

      如果判斷值不是真值,則可以這樣:

      let a;
      if ( a !== true ) {
      // do something...
      }

      簡寫:

      let a;
      if ( !a ) {
      // do something...
      }

      5.JavaScript循環(huán)簡寫方法

      for (let i = 0; i < allImgs.length; i++)

      簡寫:

      for (let index in allImgs)

      也可以使用Array.forEach

      function logArrayElements(element, index, array) {
       console.log("a[" + index + "] = " + element);
      }
      [2, 5, 9].forEach(logArrayElements);
      // logs:
      // a[0] = 2
      // a[1] = 5
      // a[2] = 9

      6.短路評價

      給一個變量分配的值是通過判斷其值是否為nullundefined,則可以:

      let dbHost;
      if (process.env.DB_HOST) {
       dbHost = process.env.DB_HOST;
      } else {
       dbHost = 'localhost';
      }

      簡寫:

      const dbHost = process.env.DB_HOST || 'localhost';

      7.十進(jìn)制指數(shù)

      當(dāng)需要寫數(shù)字帶有很多零時(如10000000),可以采用指數(shù)(1e7)來代替這個數(shù)字:

      for (let i = 0; i < 10000000; i++) {}

      簡寫:

      for (let i = 0; i < 1e7; i++) {}
      
      // 下面都是返回true
      1e0 === 1;
      1e1 === 10;
      1e2 === 100;
      1e3 === 1000;
      1e4 === 10000;
      1e5 === 100000;

      8.對象屬性簡寫

      如果屬性名與key名相同,則可以采用ES6的方法:

      const obj = { x:x, y:y };

      簡寫:

      const obj = { x, y };

      9.箭頭函數(shù)簡寫

      傳統(tǒng)函數(shù)編寫方法很容易讓人理解和編寫,但是當(dāng)嵌套在另一個函數(shù)中,則這些優(yōu)勢就蕩然無存。

      function sayHello(name) {
       console.log('Hello', name);
      }
      
      setTimeout(function() {
       console.log('Loaded')
      }, 2000);
      
      list.forEach(function(item) {
       console.log(item);
      });

      簡寫:

      sayHello = name => console.log('Hello', name);
      setTimeout(() => console.log('Loaded'), 2000);
      list.forEach(item => console.log(item));

      10.隱式返回值簡寫

      經(jīng)常使用return語句來返回函數(shù)最終結(jié)果,一個單獨語句的箭頭函數(shù)能隱式返回其值(函數(shù)必須省略{}為了省略return關(guān)鍵字)

      為返回多行語句(例如對象字面表達(dá)式),則需要使用()包圍函數(shù)體。

      function calcCircumference(diameter) {
       return Math.PI * diameter
      }
      
      var func = function func() {
       return { foo: 1 };
      };

      簡寫:

      calcCircumference = diameter => (
       Math.PI * diameter;
      )
      
      var func = () => ({ foo: 1 });

      11.默認(rèn)參數(shù)值

      為了給函數(shù)中參數(shù)傳遞默認(rèn)值,通常使用if語句來編寫,但是使用ES6定義默認(rèn)值,則會很簡潔:

      function volume(l, w, h) {
       if (w === undefined)
       w = 3;
       if (h === undefined)
       h = 4;
       return l * w * h;
      }

      簡寫:

      volume = (l, w = 3, h = 4 ) => (l * w * h);
      volume(2) //output: 24

      12.模板字符串

      傳統(tǒng)的JavaScript語言,輸出模板通常是這樣寫的。

      const welcome = 'You have logged in as ' + first + ' ' + last + '.'
      
      const db = 'http://' + host + ':' + port + '/' + database;

      ES6可以使用反引號和${}簡寫:

      const welcome = `You have logged in as ${first} ${last}`;
      
      const db = `http://${host}:${port}/${database}`;

      13.解構(gòu)賦值簡寫方法

      在web框架中,經(jīng)常需要從組件和API之間來回傳遞數(shù)組或?qū)ο笞置嫘问降臄?shù)據(jù),然后需要解構(gòu)它

      const observable = require('mobx/observable');
      const action = require('mobx/action');
      const runInAction = require('mobx/runInAction');
      
      const store = this.props.store;
      const form = this.props.form;
      const loading = this.props.loading;
      const errors = this.props.errors;
      const entity = this.props.entity;

      簡寫:

      import { observable, action, runInAction } from 'mobx';
      
      const { store, form, loading, errors, entity } = this.props;

      也可以分配變量名:

      const { store, form, loading, errors, entity:contact } = this.props;
      //最后一個變量名為contact

      14.多行字符串簡寫

      需要輸出多行字符串,需要使用+來拼接:

      const lorem = 'Lorem ipsum dolor sit amet, consectetur\n\t'
       + 'adipisicing elit, sed do eiusmod tempor incididunt\n\t'
       + 'ut labore et dolore magna aliqua. Ut enim ad minim\n\t'
       + 'veniam, quis nostrud exercitation ullamco laboris\n\t'
       + 'nisi ut aliquip ex ea commodo consequat. Duis aute\n\t'
       + 'irure dolor in reprehenderit in voluptate velit esse.\n\t'

      使用反引號,則可以達(dá)到簡寫作用:

      const lorem = `Lorem ipsum dolor sit amet, consectetur
       adipisicing elit, sed do eiusmod tempor incididunt
       ut labore et dolore magna aliqua. Ut enim ad minim
       veniam, quis nostrud exercitation ullamco laboris
       nisi ut aliquip ex ea commodo consequat. Duis aute
       irure dolor in reprehenderit in voluptate velit esse.`

      15.擴(kuò)展運算符簡寫

      擴(kuò)展運算符有幾種用例讓JavaScript代碼更加有效使用,可以用來代替某個數(shù)組函數(shù)。

      // joining arrays
      const odd = [1, 3, 5];
      const nums = [2 ,4 , 6].concat(odd);
      
      // cloning arrays
      const arr = [1, 2, 3, 4];
      const arr2 = arr.slice()

      簡寫:

      // joining arrays
      const odd = [1, 3, 5 ];
      const nums = [2 ,4 , 6, ...odd];
      console.log(nums); // [ 2, 4, 6, 1, 3, 5 ]
      
      // cloning arrays
      const arr = [1, 2, 3, 4];
      const arr2 = [...arr];

      不像concat()函數(shù),可以使用擴(kuò)展運算符來在一個數(shù)組中任意處插入另一個數(shù)組。

      const odd = [1, 3, 5 ];
      const nums = [2, ...odd, 4 , 6];

      也可以使用擴(kuò)展運算符解構(gòu):

      const { a, b, ...z } = { a: 1, b: 2, c: 3, d: 4 };
      console.log(a) // 1
      console.log(b) // 2
      console.log(z) // { c: 3, d: 4 }

      16.強制參數(shù)簡寫

      JavaScript中如果沒有向函數(shù)參數(shù)傳遞值,則參數(shù)為undefined。為了增強參數(shù)賦值,可以使用if語句來拋出異常,或使用強制參數(shù)簡寫方法。

      function foo(bar) {
       if(bar === undefined) {
       throw new Error('Missing parameter!');
       }
       return bar;
      }

      簡寫:

      mandatory = () => {
       throw new Error('Missing parameter!');
      }
      
      foo = (bar = mandatory()) => {
       return bar;
      }

      17.Array.find簡寫

      想從數(shù)組中查找某個值,則需要循環(huán)。在ES6中,find()函數(shù)能實現(xiàn)同樣效果。

      const pets = [
       { type: 'Dog', name: 'Max'},
       { type: 'Cat', name: 'Karl'},
       { type: 'Dog', name: 'Tommy'},
      ]
      
      function findDog(name) {
       for(let i = 0; i

      簡寫:

      pet = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy');
      console.log(pet); // { type: 'Dog', name: 'Tommy' }

      18.Object[key]簡寫

      考慮一個驗證函數(shù)

      function validate(values) {
       if(!values.first)
       return false;
       if(!values.last)
       return false;
       return true;
      }
      
      console.log(validate({first:'Bruce',last:'Wayne'})); // true

      假設(shè)當(dāng)需要不同域和規(guī)則來驗證,能否編寫一個通用函數(shù)在運行時確認(rèn)?

      // 對象驗證規(guī)則
      const schema = {
       first: {
       required:true
       },
       last: {
       required:true
       }
      }
      
      // 通用驗證函數(shù)
      const validate = (schema, values) => {
       for(field in schema) {
       if(schema[field].required) {
       if(!values[field]) {
       return false;
       }
       }
       }
       return true;
      }
      
      
      console.log(validate(schema, {first:'Bruce'})); // false
      console.log(validate(schema, {first:'Bruce',last:'Wayne'})); // true

      現(xiàn)在可以有適用于各種情況的驗證函數(shù),不需要為了每個而編寫自定義驗證函數(shù)了

      19.雙重非位運算簡寫

      有一個有效用例用于雙重非運算操作符。可以用來代替Math.floor(),其優(yōu)勢在于運行更快,可以閱讀此文章了解更多位運算。

      Math.floor(4.9) === 4 //true

      簡寫

      ~~4.9 === 4 //true

      看完上述內(nèi)容,你們掌握J(rèn)avaScript的簡寫寫法有哪些的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!


      分享文章:JavaScript的簡寫寫法有哪些
      文章URL:http://www.ef60e0e.cn/article/ghhhgg.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        夏邑县| 建宁县| 延安市| 班戈县| 永春县| 浦县| 若尔盖县| 紫金县| 贵阳市| 耒阳市| 霍邱县| 道孚县| 昭平县| 营口市| 乐业县| 永丰县| 宝山区| 南通市| 吐鲁番市| 怀仁县| 温泉县| 札达县| 辉南县| 馆陶县| 洞口县| 开阳县| 固原市| 阿尔山市| 南和县| 忻城县| 宁武县| 涡阳县| 清镇市| 五寨县| 邛崃市| 金华市| 古交市| 新乡市| 随州市| 搜索| 横山县|