美文网首页
观察者模式、promise基础

观察者模式、promise基础

作者: 江南之城 | 来源:发表于2019-04-28 09:57 被阅读0次

1.观察者模式

  • 发布订阅 promise redux eventBus 观察者模式(基于发布订阅的)
  • 发布 && 订阅
  • 发布订阅的特点 两者无关
  • 观察者模式 观察者 + 被观察者 (有关系的) vue双向数据绑定

1、我们需要牢记两点:①--proto--和constructor属性是对象所独有的;② prototype属性是函数所独有的,因为函数也是一种对象,所以函数也拥有--proto--和constructor属性。
2、--proto--属性的作用就是当访问一个对象的属性时,如果该对象内部不存在这个属性,那么就会去它的
--proto--属性所指向的那个对象(父对象)里找,一直找,直到-proto-属性的终点null,然后返回undefined,通过--proto--属性将对象连接起来的这条链路即我们所谓的原型链。
3、prototype属性的作用就是让该函数所实例化的对象们都可以找到公用的属性和方法,即f1.--proto-- === Foo.prototype。
4、constructor属性的含义就是指向该对象的构造函数,所有函数(此时看成对象了)最终的构造函数都指向Function。
https://blog.csdn.net/cc18868876837/article/details/81211729

2.promise基础

  • promise 用法 Promise类 静态方法(Promise.XXX) 原型上的方法(new Promise.XXX)

    1.(Promise.all Promise.resolve Promise.reject Promise.race)
    2.(then catch finally)

  • 封装promise
  • generator
 const promise = new Promise((resolve,reject)=>{
      console.log(1)
      resolve()
      console.log(2)
})
promise.then(() =>{
    console.log(3)
})
打印结果为: 1 2 3
// 解析:由于resolve()没有阻断代码的功能
    async function async1() {
        console.log( 'async1 start' )
        await async2()
        console.log( 'async1 end' )
        //await 后面的代码 是在async2中的then后面执行
    }
    async function async2() {
        console.log( 'async2' )
    }
    console.log( 'script start' )
    setTimeout( function () {
        console.log( 'setTimeout' )
    }, 0 )
    async1();
    new Promise( function ( resolve ) {
        console.log( 'promise1' )
        resolve();
    } ).then( function () {
        console.log( 'promise2' )
    } )
    console.log( 'script end' )
 运行结果如下:
     script start
     async1 start
     async2
     promise1
     script end
     promise2
     async1 end
     setTimeout
详解https://segmentfault.com/a/1190000017224799
  • async + await 详解
async function testAsync() {
    return "hello async";
}
const result = testAsync();
console.log(result);
//打印结果为是一个 Promise 对象  Promise {<resolved>: "hello async"}
当写成如下:
testAsync().then(v => {
    console.log(v);    // 输出 hello async
});

详解链接:https://segmentfault.com/a/1190000007535316

3.node

  • 让JavaScript代码运行在服务端,node 基于V8引擎 ,并不包含JavaScript的全局
  • JavaScript 有DOM BOM EcmaScript
  • 浏览器为什么不是多线程?防止同时操作dom
  • node中 基本包含EcmaScript 读写文件;提供简单、高性能服务器 (cpu密集 计算(node 不能进行复杂的计算) i/o密集 文件读取)(主线程是单线程 node API异步的) 底层还是通过多线程来模拟了异步

java 多线程 (不停的切换执行上下文
切换的很快)并发操作同一个文件 锁的概念

  • 异步/同步 被调用方 可以决定此方法是同步还是异步的
  • 阻塞和非阻塞 调用方的状态就是 阻塞或者非阻塞
  • node安装 npm node pacakge manager(安装包的)

node可以直接访问global 并且没有window的概念。
//全局属性global
//还有一些属性 exports module require __dirname __filename

1、node实现模块化(优点:命名冲突 代码方便维护 方便协作 依赖关系)。
2、浏览器中实现模块化 var obj = {a} 不能完全解决命名冲突的、调用过长、单例
3、自执行函数来实现 seajs cmd requirejs amd(异步 没人用)
4、commonjs 规范 通过文件读取(utf8)实现了模块化 1)文件即模块 2)定义了导出方式 module.exports exports 3)定义了导入方式 require
// module.exports = xxx
//exports.a = 'hello'
//

//让字符串执行?1、 eval / new Function 在浏览器中
let hello = 'zf';
eval(''console.log(hello)); //eval执行 有依赖关系
//new Function
var b=3;
let str = 'let a=1;return a+b';
let newFn = new Function('b',str);
console.log(newFn(3)) 
2、node内置模块  内置/核心   文件模块/自定义模块  第三方模块
let vm = require('vm');// node中执行字符串 可以采用vm模块
//他可以提供一个沙箱环境 运行代码  干净的环境
let hello= 'zf'
vm.runInThisContext("console.log('hello')")

//处理文件路径的 basename extname dirname join resolve

4.other

  v-if 控制的是dom   v-show控制的是样式   注:v-show不支持<template></template>


相关文章

网友评论

      本文标题:观察者模式、promise基础

      本文链接:https://www.haomeiwen.com/subject/mzwnpqtx.html