Object.observe: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 6: Line 6:
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''.
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) ==
Important: This feature is only available in the Chrome browser, either on the desktop or on an Android device.


<pre>
== Example ==
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
</pre>


== Example (JavaScript) ==
<tabber>
<pre>
JavaScript=
<syntaxhighlight lang="JavaScript">
// Object.observe Example
// Object.observe Example


Line 40: Line 27:
   console.log(chnge.type, chnge.name, chnge.oldValue))
   console.log(chnge.type, chnge.name, chnge.oldValue))
}
}
</pre>
</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
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
</syntaxhighlight>
</tabber>


== Output ==
== Output ==

Latest revision as of 22:52, 24 July 2019

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.

Important: This feature is only available in the Chrome browser, either on the desktop or on an Android device.

Example

// 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))
}

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

Output

add 0 undefined
update length 0
update 0 100 

Related Items

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