-
Notifications
You must be signed in to change notification settings - Fork 30
Object Watch
fabiantheblind edited this page Mar 13, 2014
·
1 revision
The Object Watch function is pretty awesome. You can add a watch function to any property of an object. If that property changes your function gets executed.
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
// Copyright (C) 2014 Fabian "@fabiantheblind" Morón Zirfas
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
// 0. You just DO WHAT THE FUCK YOU WANT TO.
// Object.watch() Mozilla Firefox Only.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch
//
// Node.js throws an error TypeError: Object #<Object> has no method 'watch'
//
// Works in Adobe Extendscript (woohoo!\o/)
// this test if we are in extendscript or not
if(typeof $ === 'undefined'){
// does not exist
console.log('This is a browser');
}else{
$.writeln('This is Extedscript. Creating console.log function');
var console = {
log: function(msg) {
$.writeln(msg);
}
};
}
// logger function
var logger = function(prop, oldval, newval) {
console.log("This is object: " + prop + " Old Value: " + oldval + " New Value:" + newval);
};
// create a simple object
var obj = {
a: 2,
b: 3,
c: true
};
// assign a function to all of them
for (var key in obj) {
obj.watch(key, logger);
}
// This also only works in ExtendScript
// obj.watch(["a","b","c"], function(prop, oldval, newval) {
// console.log("This is object: " + prop + " Old Value: " + oldval + " New Value:" + newval);
// });
obj.a = 5;
obj.c = false;
obj.b = null;
obj.b = []; // <-- This works in ExtendScript and fails in Firefox
obj.b.push(1);
obj.a = obj.b[0];
// awesomeThis wiki is maintained by:
fabiantheblind
Thanks to:
- JohnDarnell for fixing lots of typos.
- jsp for fixing lots of typos.
- ltfschoen for fixing typos.
- wridgers for adding more links.
Thanks to the students from the seminar for asking all those questions and making me start this wiki.
- adinaradke
- AnitaMei
- ce0311
- coerv
- felixharle
- FerdinandP
- Flave
- marche
- monkian
- natael
- OliverMatelowski
- PDXIII
- praktischend
- schlompf
- skaim
You are awesome.
- Arrays
- Classes
- Comments
- Conditionals
- Functions
- Inspect Properties
- Loops
- Objects
- Output And Interaction
- Recursive Functions
- Inspect Properties
- Variables And Operations
- Extended JavaScript Guide
- Bridge Talk
- Create And Read Files
- ExtendScript Toolkit
- File
- Folder
- Includes JSX
- Object Watch
- Read In JSON From File And DONT Eval
- Storing Data In A Target Engine
- Target an application
- XML
- app
- Colorbrewer
- Colors And Swatches
- Delay And View
- Dialogs
- Documents
- Duplicate And Transform
- Event AfterSave
- Export IDML
- ExtendScript in InDesign Scripting DOM
- Fonts
- GeometricBounds and Coordinates
- Get named pageItems
- Graphic Lines
- Groups
- HSL Color Wheel
- Images
- Includes
- InsertionPoints
- Layers
- Line Feeds And Carrige Returns
- Masterspreads
- Matrix
- Objectstyles
- Outlines Groups Alignment
- Pages And Margins
- Pathfinder
- Placeholder Text
- Rectangles Ovals Polygons
- RulerOrigin
- Select words at insertionPoint
- Simple Find And Change Grep with FC Query
- Simple Find And Change Grep
- Simple Find And Change Text
- Spiro
- Styles
- Text Analysis ID FC
- Text Analysis
- Text Find Locations
- Text
- Transformation Matricies
- TransparencySettings
- XML creation and import