最近在学习mobX的时候,探究了一下其实现机制,发现最神奇的莫过于,其中使用@observable装饰的变量的改变可以触发autorun的回调,使用了观察者模式和发布/订阅模式很类似,而这种模式反应在javascript对象上的本质就是Object.defineProperty,在很多框架中的数据双向流绑定也是这样实现的,比如Vue

该方法可以针对对象的某一个属性(key)进行设置,(设置属性的属性),主要是第三个参数比较特殊

参数描述属性类型默认值
Configurable表示能否通过delete删除属性从而重新定义属性,能否修改属性的特性数据属性、访问器属性true
Enumerable能否通过for in循环返回属性数据属性、访问器属性true
Writable能否修改该属性的值数据属性true
Value包含这个属性的数据值数据属性undefined
get在读取属性时调用的函数访问器属性undefined
set在设置属性时调用的函数访问器属性undefined

也可使用Object.defineProperties(obj,props)来定义更多属性的属性

es6新增proxy对象,顾名思义就是代理对象,通过实例化构造函数生成的对象拥有handler中配置的“功能”

参数描述
get(target, propKey, receiver)拦截对象属性的读取
set(target, propKey, value, receiver)拦截对象属性的设置,返回布尔值
has(target, propKey)拦截in操作,返回布尔值
deleteProperty(target, propKey)拦截delete操作,返回布尔值
ownKeys(target)返回一个数组。包含目标对象自身所有可遍历属性的属性名
getOwnPropertyDescriptor(target, propKey)返回属性的描述对象
defineProperty(target, propKey, propDesc)返回布尔值
preventExtensions(target)返回布尔值
getPrototypeOf(target)返回一个对象
isExtensible(target)返回布尔值
setPrototypeOf(target, proto)拦截Object.getPrototypeOf(proxy),返回一个对象
apply(target, object, args)拦截函数调用和call、apply操作
construct(target, args)拦截构造函数
  • 二者差异
  1. Object.defineProperty更偏向于针对某一个属性进行配置(针对属性),而proxy更像是对访问器属性的拓展(针对对象)
  2. 兼容性,前者更好支持到IE8,后者直接把IE干掉了
  3. proxy的set,和get支持拦截数组操作,Object.defineProperty对数组不起作用

在理解了两者以及两者的使用差异后就可以使用proxy实现 MobX 的observable和autorun

8 个评论

  1. Trump’s pressure on investigators prompted Rep.

    Zoe Lofgren, who sits on the House committee probing the insurrection, to warn that the ex-President had issued a “call to arms.”
    “Calling out for demonstrations if, you know, anything adverse, legally, happens to him, is pretty extraordinary. And I think it’s important to think through what message is being sent,” the California Democrat told CNN’s Pamela Brown on Sunday.
    In yet another sign of Trump’s incessantly consuming inability to accept his election loss, he issued a statement that same evening slamming former Vice President Mike Pence for refusing his demands to overturn the result of the democratic election in 2020, and falsely claimed that the then-vice president had the power to do so.

    Anthonysax
  2. Instagram takipçi satın al ucuz Instagram takipçi satın al türk Instagram takipçi satın al aktif Instagram takipçi satın al gerçek Instagram takipçi satın al organik Instagram takipçi satın al yabancı Instagram takipçi satın al bot Instagram takipçi satın al hızlı Instagram takipçi satın al şifresiz Instagram takipçi satın al yükle!

  3. Instagram takipçi satın hilesi sitesi güncel instagram beğeni satın hilesi şifresiz instagram takipçi satın al türk instagram takipçi satın alma bedava instagram takipçi satın alma hilesi instagram takipçi hilesi faturalı instagram takipçi satın al bayan instagram takipçi satın al gerçek instagram takipçi satın al orijinal instagram takipçi satın al 10 k instagram takipçi satın al sınırsız instagram takipçi satın al limitsiz.

  4. Instagram takipçi satın alma sitesi güncel instagram beğeni satın alma şifresiz instagram takipçi satın al türk instagram takipçi satın alma bedava instagram takipçi satın alma hilesi instagram takipçi hilesi faturalı instagram takipçi satın al bayan instagram takipçi satın al gerçek instagram takipçi satın al orijinal instagram takipçi satın al 10 k instagram takipçi satın al sınırsız instagram takipçi satın al limitsiz.

  5. Metin2 pvp serverler ile en eğlenceli pvp oyunları bulabileceğin Forumexe.tc üzerinden istediğin oyun modunu bul ve metin2 heyecanını limitsiz tat! Metin2 pvp server tanıtım forumu üzerinden ücretsiz serverleri incele, sana uygun olanını seçerek anında maceraya katıl!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注