简体   繁体   English

使用对象文字表示法设置另一个属性的属性值

[英]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.

相关问题 使用get&set将值添加到对象文字的属性中 - add value to the property of an object literal using get & set 如何拦截使用文字符号创建的unknwon对象的已知属性值分配 - How to intercept a known property value assignment of an unknwon object created using literal notation 将文字对象设置为组件的Input属性作为已知对象类 - Set literal object to a component's Input property as a known object class JavaScript:在另一个属性中使用对象的属性吗? - JavaScript: Using an object's property in another property? 对象文字属性值速记 - Object Literal Property Value Shorthand 实现一个函数来访问对象的属性,而不是使用点符号或方括号符号来访问属性? - Implementing a function to access an object's property versus accessing the property using dot notation, or bracket notation? 知道何时使用数组表示法将属性设置为 object - Know when a property has been set to an object using array notation 如何使用for循环在另一个对象中设置属性值? - How to set value of property in another object using for loop? 将一个对象的属性值设置为等于另一个对象中的相同属性 - Set the property value of an object to be equal to the same property in another object 如何在Object Literal表示法中设置其他属性时获取属性? - How to get a property when setting others in Object Literal notation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM