Eliminating some important details, I have a series of code that leads to an object with properties labeled as prop
. Afterward, I set up a $scope.$watch
to monitor the changes in the object. Subsequently, the program broadcasts two pieces of data: prop
, which represents the property name, and newValue[prop]
, indicating the value of that particular property. The process is illustrated below:
$scope.$watch(function(){return object;}, function(newValue, oldValue) {
for (var prop in object) {
if (newValue[prop] !== oldValue[prop]) {
$scope.$broadcast('from-parent', {a: prop, b: newValue[prop]});
}
}
};
The broadcasted data displays via console.log()
as
Object {a: (the property's name), b: (the value of the property)}
as expected.
However, if I remove the keys a:
and b:
like so:
$scope.$broadcast('from-parent', {prop, newValue[prop]});
An error occurs stating
Uncaught SyntaxError: Unexpected token [
.
It raises the question—Is it not permissible to reference properties using brackets within objects? Can you shed any light on this issue?