Object.observe

From NSB App Studio
Revision as of 18:43, 3 October 2014 by Ghenne (talk | contribs) (Created page with "Object.observe(''object'', ''function'') == Description == Object.observe calls ''function'' whenever there is a change to ''object''. ''Object'' can be an array, collection...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Object.observe(object, function)

Description

Object.observe calls function whenever there is a change to object. Object can be an array, collection, object, class or control.

Example (Basic)

Rem Object.observe Example
myArray = []
Object.observe(myArray, somethingChanged)
myArray[0]=100
myArray[0]=200

Function somethingChanged(changes)
  ForEach(changes, whatChanged)
End Function
  
Function whatChanged(chnge)
  console.log(chnge.type, chnge.name, .oldValue)
End Function 

Example (JavaScript)

// Object.observe Example

myArray = [];
Object.observe(myArray, somethingChanged);
myArray[0]=100;
myArray[0]=200;

function somethingChanged(changes){
  ForEach(changes, whatChanged)
}
  
function whatChanged(chnge) {
  console.log(chnge.type, chnge.name, chnge.oldValue))
}

Output

add 0 undefined
update length 0
update 0 100 

Related Items

Do...Loop, Exit, For...Next, While...Wend, For Each...Next