JavaScript provides primitive types and means of processing those. However, those are not enough. Real data must somehow come into the program and data must somehow leave the program, for it to become useful to us. In this talk, we will see how two abstractions are essential for data flow and to build up other abstractions, such as Iterator, Iterable, Observable, Scheduling, and others. Talk

Getter as abstractions:

const ten = ;

// Abstractions
// Laziness
const getTen = () => { console.log("hi"); // hook for side effects // Implementation flexibility
return + ;
// return 2 * 5;
// return 10
}

Benefits:

  • First of all: 'getTen' if you don't call the function, the calcuation inside the fucntion never run.
  • You can do any side effects inside function.
  • Implmentation flexibility, you can do different ways to aecieve the same effects.

Think about this code:

function add(getX, getY) {
const x = getX();
const y = getY();
return x + y;
}

'getX' and 'getY' are abstract. What we are doing now is adding two abstract number in concrete world. What if we add in abstract world?

function add(getX, getY) {
return () => { // put code into a get getter function
const x = getX();
const y = getY();
return x + y;
}
}

Now the function are complete lazy, lazyniess is a good thing, when you have some lzay you use getter can make things concrete.

function add(getX, getY) {
return () => {
const x = getX();
const y = getY();
return x + y;
}
} const getTen = () => ;
const getY = Math.random; const getSum = add(getTen, getY); // so far no calcuation happens

Lazy initialization and lazy iteration:

let i = ;
const array = [,,,];
function getArrayItem() {
// lazy iteration
return array[i++];
} console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem()); // undefined

This is a concrete example, we call the fucntion mutiplue time and loop though the array.

And we also see that after call result as 'undefined', if we want to loop the array again from zero idnex, we have to do:

let i = ;
const array = [,,,];
function getArrayItem() {
// lazy iteration
return array[i++];
} console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem()); i = ;
console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());

We call

i = ;

Of course this is not good at all, we leak the information.

The way to improve it is by using getter & lazyniess:

function getGetArrayItem() {
// lazy initialization
let i = ;
const array = [,,,];
return function() {
// lazy iteration
return array[i++];
}
} let getArrayItem = getGetArrayItem(); console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem()); getArrayItem = getGetArrayItem(); console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());
console.log(getArrayItem());

getter-getter: Abstract List:

Think about the following example:

function range(left, right) {

  return () => {
// lazy initialization
let x = left;
return () => {
// lazy iteration
if (x > right) {
return undefined;
}
return x++;
}
}
} const getGet = range( , );
const get = getGet(); console.log(get()); //
console.log(get());
console.log(get());
console.log(get());
console.log(get()); //
console.log(get()); // undefined
console.log(get());
console.log(get());

It prints out the range of nuber, which we defined, we can notice after the right limit of the number, it just print out undefined.

One way to solve the undefined problem is using for loop:

function range(left, right) {

  return () => {
// lazy initialization
let x = left;
return () => {
// lazy iteration
if (x > right) {
return undefined;
}
return x++;
}
}
} const getGet = range( , ); for(let get = getGet(), x = get(); x !== undefined; x = get()) {
console.log(x) // 10 ... 14
}

The good thing about this code is that no matter how large the range it is, the menory size is always 1. Every time you call the fucntion, it pull one value from the abstract getter function every time. It makes CPU and memory efficient.


Completion markers:

function range(left, right) {

  return () => {
// lazy initialization
let x = left;
return () => {
// lazy iteration
if (x > right) {
return {done: true};
}
return {done: false, value: x++};
}
}
} const getGet = range(, ); for(let get = getGet(), res = get(); !res.done; res = get()) {
console.log(res.value) // 10 ... 14
}

We added {done: true | false} as a complete marker.


Convert to Symbol.iterator:

function range(left, right) {

  return {
[Symbol.iterator]: () => {
// lazy initialization
let x = left;
return {
next: () => {
// lazy iteration
if (x > right) {
return {done: true};
}
return {done: false, value: x++};
}
}
}
}
}

Javascript notice that when you are using [Symbol.iterator] and have  'next' inside, then it provides you a nice syntax to loop over the iterator and get the value of out it.

for(let x of range(, )) {
console.log(x) // 10 ... 14
}

We might have done this:

function range(left, right) {

  return {
[Symbol.iterator]: () => {
// lazy initialization
let x = left;
return {
next: () => {
// lazy iteration
if (x > right) {
return {done: true};
}
return {done: false, value: x++};
}
}
}
}
} for(let x of range(,)) {
if(x % === ) {
console.log(x)
}
}

We using 'if' inside 'for' loop, well it is nothing wrong, but we can do better. Because range() is abstract function, we don't need to pull all the value done to the concrete world to do the filtering, we can also do the filtering in abstract function.

const filter = pred => iterations => {
let z = [];
for (let x of iterations) {
if(pred(x)) z.push(x);
}
return z;
}; function range(left, right) { return {
[Symbol.iterator]: () => {
// lazy initialization
let x = left;
return {
next: () => {
// lazy iteration
if (x > right) {
return {done: true};
}
return {done: false, value: x++};
}
}
}
}
} for(let x of filter(x => x % === )(range(,))) {
console.log(x)
}

Setter-setter abstraction:

You can think of "setter-setter" is callback:

const setSetTen = (setTen) => {
setTen()
} setSetTen(console.log) //

The benifits of doing setter-setter is

  • Async
  • Inversion of control
const setSetTen = (setTen) => {
setTimeout(() => {
//Async
setTen()
}, ) } setSetTen(console.log) //

Setter-setter to Observable:

[Javascript] Getter and Setter Abstractions的更多相关文章

  1. JavaScript getter and setter All In One

    JavaScript getter and setter All In One getter & setter JavaScript Object Accessors JavaScript A ...

  2. JavaScript getter和setter

    对象的属性是由属性名name,值key,和其他特性(可读写性 writable,可枚举性enumerable,可配置性configurable)组成的.从ES5开发,提供了getter和setter ...

  3. javascript的getter和setter(转)

    显然这是一个无关IE(高级IE除外)的话题,尽管如此,有兴趣的同学还是一起来认识一下ECMAScript5标准中getter和setter的实现.在一个对象中,操作其中的属性或方法,通常运用最多的就是 ...

  4. JavaScript中闭包实现的私有属性的getter()和setter()方法

    注意: 以下的输出都在浏览器的控制台中 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  5. javascript中的function命名空間與模擬getter、setter

    function的命名空間 在javascript中,function也可以擁有自己的命名空間例如以下這段程式碼: 12345678 function () { return 'I am A';} A ...

  6. javascript权威指南笔记--javascript语言核心(五)--getter和setter属性

    getter和setter属性: var p = { x:1.0, y:1.0, get r(){ return Math.sqrt(this.x*this.x + this.y * this.y); ...

  7. javascript中的getter和setter

    在ECMAScript 5中,属性值可以用一个或两个方法代替,这两个方法就是getter和setter var man = { name : 'lidg', weibo : '@lidg', get ...

  8. 基于 getter 和 setter 撸一个简易的MVVM

    Angular 和 Vue 在对Angular的学习中,了解到AngularJS 的两个主要缺点: 对于每一次界面时间,Ajax 或者 timeout,都会进行一个脏检查,而每一次脏检查又会在内部循环 ...

  9. js中的访问器属性中的getter和setter函数实现数据双向绑定

    嗯,之前在读js红宝书的时候,在对象那一章有介绍属性类型.第一种数据类型指的是数据属性,第二种是访问器属性.在初识vue的时候,其双向数据绑定也是基于访问器属性中的getter和setter函数原理来 ...

随机推荐

  1. How to add dependency on a Windows Service AFTER the service is installed

    his can also be done via an elevated command prompt using the sc command. The syntax is: sc config [ ...

  2. Python的学习(二十一)----Python的静态变量

    前段时间在论坛里面有人提问说, class foo(): member1 member2 ... self.member1 foo.member2 其中的两个成员member1, member2有什么 ...

  3. 洛谷P1725琪露诺(单调队列优化dp)

    P1725 琪露诺 题目描述 在幻想乡,琪露诺是以笨蛋闻名的冰之妖精.某一天,琪露诺又在玩速冻青蛙,就是用冰把青蛙瞬间冻起来.但是这只青蛙比以往的要聪明许多,在琪露诺来之前就已经跑到了河的对岸.于是琪 ...

  4. React.js初探

    React.js 菜鸟官方解释: React 是一个用于构建用户界面的 JAVASCRIPT 库. React主要用于构建UI,很多人认为 React 是 MVC 中的 V(视图). React 起源 ...

  5. 【BZOJ4590】自动刷题机

    [思路分析] 比赛的时候想到了用二分+贪心,二分的部分与贪心的部分也写对了,但是由于数据范围未看没有开long long,且二分左端点赋值过小导致WA掉 正解:二分+贪心 二分代码的长度,贪心判断能否 ...

  6. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree2

    试了一下先上再下的Treap方式,很高兴,代码变短了,但是,跑的变慢了!!!其实慢得不多,5%左右.而且这个版本的写法不容易写错..只要会一般可持久化Treap的人写着都不难...就是相对于(压行的) ...

  7. 通过委托事件实现winform窗体之间的互相刷新

    新建winform窗体Form1和Form2; 接下来要通过点击Form2的按钮,实现Form1界面的同步刷新. 先展示一下最终效果: 1.Form1界面如下: 2.点击按钮弹出Form2,界面如下: ...

  8. css选择器的综合使用

    代码实现: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf ...

  9. Android 复制文本内容到系统剪贴板(自由复制)

    直接上代码:(对应的类:android.content.ClipboardManager) //获取剪贴板管理器: ClipboardManager cm = (ClipboardManager) g ...

  10. 指定DIV局部刷新的简单实现,很简单,但是网上搜到的大部分都很复杂

    脚本部分: <script type="text/javascript"> $(function () { setInterval(function () { $(&q ...