Giter VIP home page Giter VIP logo

blog's People

Contributors

sunnyshining avatar

Watchers

 avatar  avatar  avatar

blog's Issues

js(正负)前瞻和(正负)后顾

js正则是由后面往前面读的所以,如A => B我们说B在A的前面,A在B的后面

正前瞻ab(?=abc)
/12(?=abc)/正则表达式代表12前面必须是abc,匹配结果如下
截屏2019-12-0821 11 43

负前瞻ab(?!abc)
/12(?!abc)/正则表达式代表12前面不能是abc,匹配结果如下
截屏2019-12-0821 13 12

正后顾(?<=abc)ab
/(?<=abc)12/正则表达式代表12后面必须是abc,匹配结果如下
截屏2019-12-0821 16 33

负后顾(?<!abc)ab
/(?<!abc)12/正则表达式代表12后面不能是abc,匹配结果如下
截屏2019-12-0821 18 13

位运算在react源码中的运用

在react源码中经常可以看到一些运算

workInProgress.effectTag |= PerformedWork
workInProgress.effectTag |= PerformedWork;
workInProgress.effectTag |= Update;
workInProgress.effectTag |= Hydrating;
if ((finishedWork.effectTag & Passive) !== NoEffect) {
 // pass
}
...

通过查找源码shared/ReactSideEffectTags.js得知effectTag具体会有如下值

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */

export type SideEffectTag = number;

// Don't change these two values. They're used by React Dev Tools.
export const NoEffect = /*              */ 0b0000000000000;
export const PerformedWork = /*         */ 0b0000000000001;

// You can change the rest (and add more).
export const Placement = /*             */ 0b0000000000010;
export const Update = /*                */ 0b0000000000100;
export const PlacementAndUpdate = /*    */ 0b0000000000110;
export const Deletion = /*              */ 0b0000000001000;
export const ContentReset = /*          */ 0b0000000010000;
export const Callback = /*              */ 0b0000000100000;
export const DidCapture = /*            */ 0b0000001000000;
export const Ref = /*                   */ 0b0000010000000;
export const Snapshot = /*              */ 0b0000100000000;
export const Passive = /*               */ 0b0001000000000;
export const Hydrating = /*             */ 0b0010000000000;
export const HydratingAndUpdate = /*    */ 0b0010000000100;

// Passive & Update & Callback & Ref & Snapshot
export const LifecycleEffectMask = /*   */ 0b0001110100100;

// Union of all host effects
export const HostEffectMask = /*        */ 0b0011111111111;

export const Incomplete = /*            */ 0b0100000000000;
export const ShouldCapture = /*         */ 0b1000000000000;

可以看到大部分的值都只有一位是1,其他位都是0

假设当前的effectTag为Update,

// Don't change these two values. They're used by React Dev Tools.
var NoEffect = /*              */ 0b0000000000000;
var PerformedWork = /*         */ 0b0000000000001;

// You can change the rest (and add more).
var Placement = /*             */ 0b0000000000010;
var Update = /*                */ 0b0000000000100;
var PlacementAndUpdate = /*    */ 0b0000000000110;
var Deletion = /*              */ 0b0000000001000;
var ContentReset = /*          */ 0b0000000010000;
var Callback = /*              */ 0b0000000100000;
var DidCapture = /*            */ 0b0000001000000;
var Ref = /*                   */ 0b0000010000000;
var Snapshot = /*              */ 0b0000100000000;
var Passive = /*               */ 0b0001000000000;
var Hydrating = /*             */ 0b0010000000000;
var HydratingAndUpdate = /*    */ 0b0010000000100;

// Passive & Update & Callback & Ref & Snapshot
var LifecycleEffectMask = /*   */ 0b0001110100100;

// Union of all host effects
var HostEffectMask = /*        */ 0b0011111111111;

var Incomplete = /*            */ 0b0100000000000;
var ShouldCapture = /*         */ 0b1000000000000;

var effectTag = Update

effectTag |= Ref // effectTag此时为0b0000010000100

console.log((effectTag & Update) === Update) // true
console.log((effectTag & Ref) === Ref) // true
console.log((effectTag & Placement) === Placement) // false
effectTag &= ~Ref
console.log((effectTag & Ref) === Ref) // false

&操作是用来判断在某个变量中是否含有某个属性的

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.