Options
All
  • Public
  • Public/Protected
  • All
Menu

observe

a observe for all type variables

本项目基于 dntzhang/oba 改造而来,原项目地址:https://github.com/dntzhang/oba

对象字面量

var obj = { a: 1 }
observe(obj, function(name, value, old) {
  console.log(name + '__' + value + '__' + old)
})
obj.a = 2 //a__2__1

数组

var arr = [1, 2, 3]
observe(arr, function(name, value, old) {
  console.log(name + '__' + value + '__' + old)
})
arr.push(4) //Array-push__[1,2,3,4]__[1,2,3]
arr[3] = 5 //3__5__4

复杂对象

var complexObj = { a: 1, b: 2, c: [{ d: [4] }] }
observe(complexObj, function(name, value, old, path) {
  console.log(name + '__' + value + '__' + old) //d__100__4
  console.log(path) //#-c-0
})
complexObj.c[0].d = 100

普通对象

var User = function(name, age) {
  this.name = name
  this.age = age
  //只监听name
  observe(this, ['name'], function(name, value, oldValue) {
    console.log(name + '__' + value + '__' + oldValue)
  })
}
var user = new User('lisi', 25)
user.name = 'wangwu' //name__wangwu__lisi
user.age = 20 //什么都输出,因为没有监听age

Index

Variables

Let methods

methods: Array<string> = ['concat','copyWithin','entries','every','fill','filter','find','findIndex','forEach','includes','indexOf','join','keys','lastIndexOf','map','pop','push','reduce','reduceRight','reverse','shift','slice','some','sort','splice','toLocaleString','toString','unshift','values','size']

Let triggerStr

triggerStr: string = ['concat','copyWithin','fill','pop','push','reverse','shift','sort','splice','unshift','size'].join(',')

Functions

_getRootName

  • _getRootName(prop: string, path: string): string
  • 获取该属性的上一级路径,默认是"#"

    Parameters

    • prop: string

      属性值

    • path: string

      路径

    Returns string

isArray

  • isArray(obj: any): boolean
  • 判断是否为数组

    Parameters

    • obj: any

      任意对象变量

    Returns boolean

isFunction

  • isFunction(o: any): boolean
  • 判斷是否是函數 Determine if it is a function

    Parameters

    • o: any

      函數

    Returns boolean

isInArray

  • isInArray(arr: Array<any>, item: any): boolean
  • 判断一个东西是否存在于数组内

    Parameters

    • arr: Array<any>

      数组

    • item: any

      子项

    Returns boolean

isString

  • isString(o: any): boolean
  • 判斷是不是字符串 Determine if it is a string

    Parameters

    • o: any

      字符串

    Returns boolean

observe

  • observe(target: any, arr?: string | Array<string>, callback?: Function): void
  • 用于观察值变化并触发回调

    Parameters

    • target: any

      观察对象

    • Optional arr: string | Array<string>

      观察属性或观察属性数组

    • Optional callback: Function

      触发回调

    Returns void

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc