如何在javascript对象中访问内部对象的属性

How to access attribute of an inner object in javascript object

本文关键字:访问 内部对象 属性 对象 javascript      更新时间:2023-09-26

下面是我的代码:

function TaskRepository () {
    var self = this;
    var entity = {
        'startTime': 900,
        'endTime': 1830
    };
    this.setStartTime = function(sTime){
        self.entity.startTime = sTime;
    };
    this.getStartTime = function(){
        return self.entity.startTime;
    };
}

但是下面的代码不能工作

var x= new TaskRepository();
x.setStartTime(6);

怎么了?我错过了什么?我也试图通过self.entity['startTime']访问属性,但这也不工作。

由于您将函数用作构造函数,因此必须将实体设置为属性,而不是伪私有变量。如果你打算做很多这样的taskRepos,你可以把这两个方法移到原型中。

function TaskRepository () {
    this.entity = {
        'startTime': 900,
        'endTime': 1830
    };
    this.setStartTime = function(sTime){
        this.entity.startTime = sTime;
    };
    this.getStartTime = function(){
        return this.entity.startTime;
    };
}

function TaskRepository () {
    this.entity = {
        'startTime': 900,
        'endTime': 1830
    };
}
TaskRepository.prototype = {
    'setStartTime' : function(sTime) {
        this.entity.startTime = sTime;
    },
    'getStartTime' : function(){
        return this.entity.startTime;
    }
};

问题描述:

var x= new TaskRepository();

X是TaskRepository的实例。

var self = this;

Self是X (TaskRepository的实例)中引用实例的局部变量。

var entity = {
        'startTime': 900,
        'endTime': 1830
    };

entity是实例中的一个局部变量,它与实例无关,因此你不能使用实例访问它。如下图所示。

self.entity.startTime

解决方案:

删除self (=> entity.startTime)或执行以下操作:

this.entity = {
        'startTime': 900,
        'endTime': 1830
    };

编辑以更好地理解:

function TaskRepository () {
    //THIS refers to the instance of TaskRepository that we named X
    //We don't need SELF since we are already within the instance and it's easy to access THIS
    //Entity is now part of any TaskRepository instances
    this.entity = {
        'startTime': 900,
        'endTime': 1830
    };
    this.setStartTime = function(sTime){
        //THIS refers to the instance of TaskRepository because we are in a method that is part of the instance (Hint it has THIS.setStartTime)
        //To access entity you use the context this
        this.entity.startTime = sTime;
    };
    this.getStartTime = function(){
        return this.entity.startTime;
    };
}

entity不是您使用new TaskRepository创建的对象的属性,它是由调用new TaskRepository的上下文保存的局部变量。由于在调用期间创建的函数在调用完成后继续存在,因此该上下文保持活动状态,并且您可以继续从这些函数中访问它(它们是闭包,因此它们保持上下文活动)。

因为entity不是一个属性,你不能通过实例访问它。它是一个变量,你的函数靠近它,你可以直接使用它:

function TaskRepository () {
    var self = this;  // <=== Unless you use this for something you haven't shown,
                      //      you can remove it
    var entity = {
        'startTime': 900,
        'endTime': 1830
    };
    this.setStartTime = function(sTime){
        entity.startTime = sTime;
    //  ^---------- no 'self'
    };
    this.getStartTime = function(){
        return entity.startTime;
    //         ^---------- no 'self'
    };
}

每次调用new TaskRepository都会创建一个新的上下文,因此每个调用都有自己的entity副本,因此它最终是特定于对象的,即使它不是对象的属性。

这是为实例创建"私有"属性的经典方法。


旁注:在创建entity的对象初始化器中,属性名上的单引号是不必要的。无害,但没有必要。

@Shilly的答案工作得很好,但它会使entity变量公开。如果你想让它保持私有,只需使用你的代码,但通过self:

删除对entity的引用。
function TaskRepository () {
    var entity = {
        'startTime': 900,
        'endTime': 1830
    };
    this.setStartTime = function(sTime){
        entity.startTime = sTime;
    };
    this.getStartTime = function(){
        return entity.startTime;
    };
}