[英]Using object literal notation to set a property's value in terms of another property
I encounter an error in the following code because of the way I try to define the 'scanning' property on my reader object. 由于尝试在读取器对象上定义'scanning'属性的方式,我在以下代码中遇到错误。
app.controller("turingController", ["$scope", function($scope) {
$scope.cellArray = [];
for( var i=0; i<=999; i++) {
$scope.cellArray[i] = {
index: i,
content: "B",
};
};
$scope.reader = {
location: 500,
state: "q1",
scanning: $scope.cellArray[$scope.reader.location].content
};
}]);
But if I don't use object literal notation to define the 'scanning' property, it works fine. 但是,如果我不使用对象文字表示法来定义“扫描”属性,它就可以正常工作。 Like this:
像这样:
$scope.reader = {
location: 500,
state: "q1",
};
$scope.reader.scanning = $scope.cellArray[$scope.reader.location].content
I realize that I've solved the problem, but I'm still wondering why it doesn't work in the first code? 我意识到我已经解决了问题,但是我仍然想知道为什么它在第一个代码中不起作用? Thanks in advance!
提前致谢!
When you are defining $scope.reader
in the first example, you are also calling $scope.reader
(actually $scope.reader.location
) within the definition. 在第一个示例中定义
$scope.reader
时,还将在定义内调用$scope.reader
(实际上是$scope.reader.location
)。 At the point you call $scope.reader.location
you have not finished defining $scope.reader
. 此时,您调用
$scope.reader.location
尚未定义$scope.reader
。 So it gets confused... 所以很困惑...
In the second method, you first define the $scope.reader
object, then add a new property ( scanning
) to it, setting the value based in part on $scope.reader
. 在第二种方法中,首先定义
$scope.reader
对象,然后一个新的属性(添加scanning
)给它,根据设置在上部分的价值$scope.reader
。 This is fine since the $scope.reader
object now exists 很好,因为
$scope.reader
对象现在存在
At the point in which you construct the object literal for $scope.reader
, you haven't actually defined $scope.reader
yet — you're trying to reference a variable that does not exist yet. 在为
$scope.reader
构造对象文字时,您实际上尚未定义$scope.reader
-您正在尝试引用尚不存在的变量。
However, by setting it afterwards, in your new code, $scope.reader
has been set, so you can reference it without any issues. 但是,通过在以后的新代码中进行设置,已经设置了
$scope.reader
,因此您可以毫无问题地引用它。
This is javascript problem, not really angularjs. 这是javascript问题,不是angularjs。
location
doesn't exist until the following line $scope.reader = {};
直到下一行
$scope.reader = {};
location
才存在$scope.reader = {};
is finished. 完成。
It is something along this line: 这是沿着这条线:
var A = 100;
var B = A = B;
//What is the value of B?
//Does (B = A) execute first, so B = 100?
//Does (A = B) execute first, so B = undefined?
//answer, B is undefined.
//this is analogy to your case
// var R.A = 100 never happens until R = {} is finished.
// so B = A is undefined
var R = {
A: 100,
B: A
};
The expression is evaluated prior to assignment. 在赋值之前对表达式求值。 You statement is like this:
您的声明是这样的:
var temp = {
location: 500,
state: "q1",
scanning: $scope.cellArray[$scope.reader.location].content
};
$scope.reader = temp;
So when the property is being assigned a value, $scope.reader
has not yet been set. 因此,在为属性分配值时,尚未设置
$scope.reader
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.