✒️
Notes
  • Lirenxn's daily notes
  • Featured
    • Concurrent on fiber and requestIdleCallback
    • Deep dive eslint
      • eslint with class property
      • eslint with fix
      • eslint with jsx
    • A opinioned intro to React Hooks
      • React Hooks 1 - An Overview
      • React Hooks 2 - A mindset change
      • React Hooks 3 - Test Hooks
    • Handy tricks
    • About Micro FrontEnd
    • Same-site cookie
    • Thoughts
      • Application Security for frontend OWASP
      • Javascript this
      • React validation
  • 2020 Daily Notes
    • Notes-04-05-eslint-and-babel
    • Notes-04-03-Hard-interview-question
    • Notes-03-31-ReactContext-JS-inherit
    • Notes-03-28-digital-signing-webpack-dynamic-import-JsonRest-Promise-pattern-performance.measure()
    • Wk-notes-03-16-i18n-solution
    • Notes-03-15-Concurrent-with-requestIdleCallback
    • Notes-03-15-Micro-frontend
    • Wk-notes-03-09-micro-frontend-bootstrapper
    • Wk-notes-02-27-API-design-(g)RPC-REST-etc
    • Wk-notes-02-26-React-form-validation-Boolean-algebra-Rule-for-this-App-security-npm-devtool-tricks
    • Wk-notes-02-18-i18n-gRPC
    • Wk-notes-02-11-Gradual-rollout-webpack-vscode-auto-import
    • Note-02-09-spring-webpack
    • Wk-notes-02-06-more-webpack
    • Wk-notes-02-05-Learn-spring-webpack
    • Wk-notes-02-04-props-drilling-virtual-list
    • Wk-notes-02-03-same-site-cookie
    • Wk-notes-01-31-g-recaptcha-npmshrinkwrap
    • Wk-notes-01-30-React-ref
    • Wk-notes-01-23-remote-android
    • Wk-notes-01-23-test-hook
    • Wk-notes-01-22-about-Hooks
    • Wk-notes-01-17-bundling-browser-module-treeshake-2
    • Wk-notes-01-16-Bundling-Treeshaking-1
    • Wk-notes-01-13-npm-script-css-scroll
    • Wk-notes-01-13-touchscreen-hover-and-apis
    • Wk-notes-01-13-emotion-x
    • Wk-notes-01-10-codemod
    • Wk-notes-01-08-live-region-react-test-lib
    • Wk-notes-01-06-eslint-finalise
  • 2019 Daily Notes
    • Wk-notes-12-11-eslint-dive-vscode-debug
    • Wk-notes-12-10-eslint-dive
    • Wk-notes-12-6-splunk
    • Wk-notes-12-3-react-function-memo
    • Wk-notes-12-2-agile-test-micro-frontend
    • Wk-notes-11-29-npm-fix-aem-datalayer-test
    • Wk-notes-11-28-adobe-dataLayer
    • Wk-notes-11-27-a11y
    • Wk-notes-11-25-upgrade-preact
    • Wk-notes-11-22-a11y-n-voice-over
    • Wk-notes-11-21-Add-Typescript
    • Wk-notes-11-20-JSDoc
    • Wk-notes-11-19-A11y-Polyfill
    • Wk-notes-11-18-jest-mock
    • Wk-notes-11-15-React-Portal
    • Wk-notes-11-14-iOS-simulator-git-hooks-All-hands-testing
    • Wk-notes-11-13-i18n
    • Wk-notes-11-12-safari-remote-debug
    • Wk-notes-11-11-migrating-typescript-git-remote-label-Emotion-controlled-component
    • Wk-notes-11-08-living-pricing-arch
    • Wk-notes-11-07-vitual-box-n-onblur-for-div
    • Wk-notes-11-06-bug-race-virtual-dom-diff
    • Wk-notes-11-05-babel-loader-eslint
Powered by GitBook
On this page
  • React Context
  • Javascript inheritance in ES5

Was this helpful?

  1. 2020 Daily Notes

Notes-03-31-ReactContext-JS-inherit

PreviousNotes-04-03-Hard-interview-questionNextNotes-03-28-digital-signing-webpack-dynamic-import-JsonRest-Promise-pattern-performance.measure()

Last updated 5 years ago

Was this helpful?

React Context

  • React.creatContext() is called only once, multiple context objects is handled by Provider call

  • <Context.Provider> will create a new instance internally in the Context and separate it with other Provider from the same Context object

  • useContext/<Context.Consumer> grab the closest Provider and consumer its value only

Javascript inheritance in ES5

Ref:

Keys:

  • use Object.create() to create new object as prototype

  • set Child.prototype to inherit (extends)

  • reset Child.prototype.constructor to keep correct constructor

  • use Parent() WITHOUT new as super()

  • Object.getPrototypeOf() to properly find prototype

Proper inheritance

function Base(name, group){
  this.name = name;
  this.group = group;
}

// To define public inheritable function
Base.prototype.getPath = function (){
  return this.group + '.' + this.name;
}

function Future(init, opt, extra){
  this.resolved = undefined;
  // To keep `this` in `done()` function
  const self = this;
  function done(v){
    self.resolved = v;
  }
  if(opt){
    // To pass the correct 'this' to `super()`
    Base.call(self, opt.name, opt.group);
  }
  init(done, ...extra);
}

// To inherit properly with correct constructor
Future.prototype = Object.create(Base.prototype);
Future.prototype.constructor = Future;

// To define public inheritable function
Future.prototype.then = function(cb){
  this.resolved = cb(this.resolved) || this.resolved;
}

function RejectableFuture(init, opt){
  this.rejected = undefined;
  // To keep `this` in `done()` function
  const self = this;
  function wrong(v){
    self.rejected = v;
  }
  // To pass the correct 'this' to `super()`
  Future.call(self, init, opt, [wrong]);
}

// To inherit properly with correct constructor
RejectableFuture.prototype = Object.create(Future.prototype);
RejectableFuture.prototype.constructor = RejectableFuture;

// To define public inheritable function
RejectableFuture.prototype.catch = function(cb){
  try{
    this.resolved = cb(this.rejected) || this.resolved;
  }catch(rejected){
    this.rejected = rejected;
  }
}

// To overwrite parent function
RejectableFuture.prototype.then = function(cb){
  try{
    this.resolved = cb(this.resolved) || this.resolved;
  }catch(rejected){
    this.rejected = rejected;
  }
}

rft = new RejectableFuture(
  (done, wrong) => {
    console.log('done n wrong');
    done(1);
    wrong(2)
  }, 
  { 
    name:'doneNwrong', 
    group:'ulti' 
  }
);

rft.then(console.log);
// 1
rft.catch(console.log)
// 2
rft.then(r => { console.log(r); return `return(${r})`; });
// 1
rft.then(console.log);
// return(1)
rft.catch(r => { console.log(r); throw `throw(${r})`; });
// 2
rft.catch(console.log);
// throw(2)
rft.getPath();
// ulti.doneNwrong

The protorype chain

RejectableFuture.prototype === Object.getPrototypeOf(rft);
// Future {constructor: RejectableFuture, catch, then }

Future.prototype !== Object.getPrototypeOf(Object.getPrototypeOf(rft));
// Base {constructor: Future, then}

Future.prototype !== Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(rft)));
// { constructot: Base, getPath }

Use Prototype public function in constructor

This is similar to react component constructor handler binding

function Future(init, opt, extra){
  this.resolved = undefined;
  if(opt){
    Base.call(this, opt.name, opt.group);
  }
  // To use the public function, while bind is required
  init(this.done.bind(this), ...extra);
}

Future.prototype = Object.create(Base.prototype);
Future.prototype.constructor = Future;

Future.prototype.then = function(cb){
  this.resolved = cb(this.resolved) || this.resolved;
}

Future.prototype.done = function done(v){
  this.resolved = v;
}
http://www.objectplayground.com/