Object.observe: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
Object.observe(''object'', ''function'')
Object.observe(''object'', ''function'')
Object.unobserve(''object'')


== Description ==
== Description ==


Object.observe calls ''function'' whenever there is a change to ''object''. ''Object'' can be an array, collection, object, class or control. This can be very useful in debugging, to watch the value of an object change during runtime.
Object.observe calls ''function'' whenever there is a change to ''object''. ''Object'' can be an array, collection, object, class or control. This can be very useful in debugging, to watch the value of an object change during runtime. Object.unobserve stops calling ''function''.


== Example (Basic) ==
== Example (Basic) ==

Revision as of 18:49, 3 October 2014

Object.observe(object, function) Object.unobserve(object)

Description

Object.observe calls function whenever there is a change to object. Object can be an array, collection, object, class or control. This can be very useful in debugging, to watch the value of an object change during runtime. Object.unobserve stops calling function.

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, chnge.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