12 nga tinihanga JavaScript e ngaro ana i te nuinga o nga akoranga

12 nga tinihanga JavaScript e ngaro ana i te nuinga o nga akoranga

I taku tiimata ki te ako i te JavaScript, ko te mea tuatahi i mahia e au ko te hanga rarangi o nga mahi tinihanga i awhina i ahau ki te penapena wa. I kitea e au mai i etahi atu hötaka, i nga waahi rereke me nga pukapuka.

I roto i tenei tuhinga, ka whakaatu ahau ki a koe 12 nga huarahi pai hei whakapai ake me te tere ake i to waehere JavaScript. I te nuinga o te waa he ao katoa.

Ka whakamahara matou: mo nga kaipānui katoa o "Habr" - he utu mo te 10 rubles i te wa e whakauru ana ki tetahi akoranga Skillbox ma te whakamahi i te waehere whakatairanga "Habr".

Ka tūtohu a Skillbox: Akoranga mahi "Whakawhanake Waea PRO".

Tātari uara ahurei

RANGATIRA

Ko te momo ahanoa Tautuhi i whakauruhia ki roto i te ES6, me te..., ka horahia te kaiwhakahaere, ka taea e tatou te whakamahi hei hanga i tetahi huinga hou kei roto i nga uara ahurei anake.

const array = [1, 1, 2, 3, 5, 5, 1]
const uniqueArray = [...new Set(array)];
 
console.log(uniqueArray); // Result: [1, 2, 3, 5]

I roto i te ahuatanga noa, he nui ake te waehere hei mahi i taua mahi.

Ka mahi tenei tikanga mo nga hurangi kei roto nga momo o mua: te kore tautuhi, te null, te boolean, te aho me te tau. Mēnā kei te mahi koe me tētahi huānga kei roto he ahanoa, he mahi, he huānga atu rānei, me whai huarahi kē koe.

Te roa o te huinga keteroki i roto i nga huringa

HEIHORA

Ina ako koe mo nga koropiko, ka whai koe i te tikanga paerewa:

for (let i = 0; i < array.length; i++){
  console.log(i);
}

Heoi, ma tenei wetereo, ka tirotirohia e te kohanga mo te roa o te huranga ia whitiwhitinga.

I etahi wa ka whai hua tenei, engari i te nuinga o te waa he pai ake te keteroki i te roa o te huinga, me uru mai tetahi ki reira. Ka taea e tatou tenei ma te tautuhi i te taurangi roa ka tautuhia te taurangi i, penei:

for (let i = 0, length = array.length; i < length; i++){
  console.log(i);
}

Ko te tikanga, he rite tonu ki runga ake nei, engari ma te whakanui ake i te rahi o te kapiti ka nui te penapena wa.

Whakatauranga iahiko poto (whakatauranga McCarthy)

KAUPAPA KAUPAPA

Ko te kaiwhakahaere matua he huarahi tere me te pai ki te tuhi i nga korero herenga ngawari (me etahi wa kaore i te ngawari):

x> 100? “neke atu i te 100”: “iti iho i te 100”;
x> 100? (x>200? "neke atu i te 200": "i waenganui i te 100-200"): "iti iho i te 100";

Engari i etahi wa ka nui ake te uaua o te kaiwhakahaere ternary i te mea e hiahiatia ana. Ka taea e tatou te whakamahi 'me' && me te 'ranei' hei utu || Ko nga kaiwhakahaere Boolean ki te arotake i etahi korero i roto i te huarahi poto ake. He maha nga wa e kiia ana he "iaiawhio poto" he "whakatauranga iahiko poto".

Nahea teie ohipa

Me kii e hiahia ana matou ki te whakahoki mai i tetahi o nga tikanga e rua, neke atu ranei.

Ma te whakamahi && ka whakahokia te uara teka tuatahi. Mēnā ka aro mātai ia operand ki te pono, kātahi ka whakahokia te kīanga whakamutunga i arotakehia.

let one = 1, two = 2, three = 3;
console.log(one && two && three); // Result: 3
 
console.log(0 && null); // Result: 0

Ma te whakamahi || ka whakahokia te uara pono tuatahi. Mēnā he hē te arotakenga o ia mahi, ka whakahokia te uara arotake whakamutunga.

let one = 1, two = 2, three = 3;
console.log(one || two || three); // Result: 1
 
console.log(0 || null); // Result: null

tauira 1

Me kii e hiahia ana tatou ki te whakahoki i te roa o te taurangi, engari karekau e mohio ki tona momo.

I tenei keehi, ka taea e koe te whakamahi mena/kei te tirotiro ko te foo te momo tika, engari he roa rawa pea tenei tikanga. Na reira, he pai ake te tango i ta tatou "whakawhitiwhiti poto".

return (foo || []).length;

Mena he pai te roa o te taurangi foo, ka whakahokia mai. Ki te kore ka whiwhi tatou i te 0.

tauira 2

Kua raru koe ki te uru atu ki tetahi ahanoa kohanga? Kare pea koe e mohio kei te noho tonu tetahi ahanoa, tetahi ranei o ona ahuatanga iti, ka raru pea.

Hei tauira, i pirangi matou ki te uru ki te taonga raraunga i roto i tenei.state, engari kaore i te tautuhia te raraunga kia whakahokia mai ra ano e ta matou papatono he tono tiki.

I runga i te waahi e whakamahia ana e maatau, ma te waea atu ki tenei.state.data ka kore e tiimata te tono. Hei whakatau i te rapanga, ka taea e tatou te takai i tenei ki te kupu here:

if (this.state.data) {
  return this.state.data;
} else {
  return 'Fetching Data';
}

Ko te whiringa pai ake ko te whakamahi i te kaiwhakahaere "ranei".

return (this.state.data || 'Fetching Data');

Kaore e taea te whakarereke i te waehere i runga ake nei hei whakamahi &&. Ko te 'Tikina Raraunga' && tenei.state.data kaiwhakahaere ka whakahoki tenei.state.data ahakoa he kore tautuhia, kaore ranei.

mekameka kōwhiringa

Ka whakaaro pea tetahi ki te whakamahi i nga mekameka hei whakahoki i tetahi rawa ki roto i te hanganga rakau. Na, ko te tohu tohu patai? ka taea te whakamahi ki te tiki taonga mena karekau noa.

Hei tauira, ka taea e tatou te whakahoki ano i te tauira i runga ake nei kia whiwhi tenei.state.data?..(). Arā, ka whakahokia te raraunga mena karekau te uara.

Ranei, mena he mea nui mena kua tautuhia te kawanatanga, kaore ranei, ka taea e matou te whakahoki tenei.state?.data.

Tahuri ki Boolean

WHAKAARO MOMO

I tua atu i nga mahi boolean noa he pono me te teka, ka tukuna ano e JavaScript etahi atu uara he pono, he teka ranei.

Kia tae ra ano ki etahi atu korero, he pono nga uara katoa i roto i te JavaScript, haunga te 0, "", null, undefined, NaN me, he pono, he teka. He teka nga mea o muri.

Ka taea e taatau te huri i waenga i nga mea e rua ma te whakamahi i te! operator, ka huri ano i te momo ki te boolean.

const isTrue  = !0;
const isFalse = !1;
const alsoFalse = !!0;
 
console.log(true); // Result: true
console.log(typeof true); // Result: "boolean"

Tahuri ki te aho

WHAKAARO MOMO

Ko te huringa tere mai i te tauoti ki te aho ka taea te mahi penei.

const val = 1 + "";
 
console.log(val); // Result: "1"
console.log(typeof val); // Result: "string"

Hurihia ki te tauoti

WHAKAARO MOMO

Ka mahia e matou te huringa whakamuri penei.

let int = "15";
int = +int;
 
console.log(int); // Result: 15
console.log(typeof int); Result: "number"

Ka taea hoki te whakamahi i tenei tikanga ki te huri i te momo raraunga boolean ki nga uara tau, penei i raro nei:

console.log(+true);  // Return: 1
console.log(+false); // Return: 0

He waahi pea ka whakamaoritia a + hei kaiwhakahaere whakakotahi, kaua ko te kaiwhakahaere taapiri. Hei karo i tenei, me whakamahi tildes: ~~. He rite tenei kaiwhakahaere ki te -n-1. Hei tauira, ~15 he rite ki te -16.

Ma te whakamahi i nga tire e rua i te rarangi ka whakakore i te mahi na te mea - (- - n - 1) - 1 = n + 1 - 1 = n. Arā, ~-16 he rite ki te 15.

const int = ~~"15"
console.log(int); // Result: 15
console.log(typeof int); Result: "number"

<Nga Mana Tere

NGA WHAKAMAHI

Ka timata mai i te ES7, ka taea e koe te whakamahi i te kaiwhakahaere tautohu ** hei tohu poto mo nga mana. He tere ake tenei i te whakamahi Math.pow(2, 3). He ahua ngawari, engari ko tenei waahanga kei roto i te rarangi o nga tikanga, na te mea kaore i te whakahuahia i nga waahi katoa.

console.log(2 ** 3); // Result: 8

Kaua e pohehe ki te ^ tohu, e whakamahia nuitia ana mo te whakareatanga. Engari i roto i te JavaScript koinei te kaiwhakahaere XOR.

I mua i te ES7, ko te ** pokatata ka taea anake te whakamahi mo nga mana o te turanga 2 ma te whakamahi i te kaiwhakahaere nekehanga maaiti maui <<:

Math.pow(2, n);
2 << (n - 1);
2**n;

Hei tauira, 2 << 3 = 16 he rite ki te 2 ** 4 = 16.

Mānu ki te tauoti

NGĀ WHAKAMAHI / WHAKAARO MOMO

Mena ka hiahia koe ki te huri i te maanu ki te tauoti, ka taea e koe te whakamahi i te Math.floor(), Math.ceil() or Math.round(). Engari he huarahi tere ake, mo tenei ka whakamahia e matou |, ara, te kaiwhakahaere OR.

console.log(23.9 | 0);  // Result: 23
console.log(-23.9 | 0); // Result: -23

Whanonga | ka whakawhirinaki te nuinga ki te mahi koe ki nga tau pai, kino ranei, no reira ka pai noa tenei tikanga mena ka maia koe ki taau mahi.

n | Ka tangohia e 0 nga mea katoa i muri i te whakawehe ira, ka tapahia te maanu ki te tauoti.

Ka taea e koe te whiwhi i taua awenga whakaawhiwhi ma te whakamahi ~~. Whai muri i te hurihanga kaha ki te tauoti, ka noho tonu te uara kaore i te rereke.

Te tango i nga tau whai muri

Ka taea te whakamahi i te kaiwhakahaere OR ki te tango i te maha o nga mati mai i tetahi tau. Ko te tikanga karekau e hiahia ki te huri momo penei i konei:

let str = "1553";
Number(str.substring(0, str.length - 1));

Engari ka tuhi noa tatou:

console.log(1553 / 10   | 0)  // Result: 155
console.log(1553 / 100  | 0)  // Result: 15
console.log(1553 / 1000 | 0)  // Result: 1

Te hono aunoa

KARANGA

Ka taea te whakamahi tohu pere ES6 i roto i nga tikanga karaehe, ka tohuhia te here. Ko te tikanga ka taea e koe te poroporoaki ki nga korero tukurua penei.myMethod = this.myMethod.bind(this)!

import React, { Component } from React;
 
export default class App extends Compononent {
  constructor(props) {
  super(props);
  this.state = {};
  }
 
myMethod = () => {
    // This method is bound implicitly!
  }
 
render() {
    return (
      <>
        <div>
          {this.myMethod()}
        </div>
      </>
    )
  }
};

Te kuti huinga

RANGATIRA

Mena ka hiahia koe ki te tango i nga uara mai i te raupapa, he tere ake nga tikanga i te splice().

Hei tauira, ki te mohio koe ki te rahi o te huinga taketake, ka taea e koe te whakakore i tona taonga roa penei:

let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array.length = 4;
 
console.log(array); // Result: [0, 1, 2, 3]

Engari tera ano tetahi tikanga, he tere ake. Mena ko te tere te mea nui ki a koe, koinei ta matou whiringa:

let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array = array.slice(0, 4);
 
console.log(array); // Result: [0, 1, 2, 3]

Te tā i te/ngā uara whakamutunga o te huānga

RANGATIRA
Ko tenei tikanga me whakamahi te tikanga poro().

let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
 
console.log(array.slice(-1)); // Result: [9]
console.log(array.slice(-2)); // Result: [8, 9]
console.log(array.slice(-3)); // Result: [7, 8, 9]

Whakahōputu waehere JSON

JSON

Kua whakamahi kē koe i a JSON.stringify. I mohio koe ka awhina i te whakahōputu i to JSON?

Ko te tikanga stringify() e rua nga tawhā whiriwhiri: he mahi whakakapi, ka taea te whakamahi hei tātari i te JSON kua whakaatuhia, me te uara mokowhiti.

console.log(JSON.stringify({ alpha: 'A', beta: 'B' }, null, 't'));
 
// Result:
// '{
//     "alpha": A,
//     "beta": B
// }'

Heoi ano, ko taku tumanako i whai hua enei tikanga katoa. He aha nga tinihanga e mohio ana koe? Tuhia ki roto i nga korero.

Ka tūtohu a Skillbox:

Source: will.com

Tāpiri i te kōrero