访问实例方法中的实例属性

时间:2015-10-05 06:57:12

标签: javascript parse-platform

我试图扩展一个Object并为其添加一些自定义实例方法。如何访问属性中的数据?

const Media = Parse.Object.extend('Media', {
  getUrl: function getUrl() {
    // HOW DO I ACCESS ATTRIBUTES HERE?
  },
});

const url = new Media({
  url: 'url here',
}).getUrl()

我基本上希望属性值存在于this或this.get(' url')

那么如何访问实例方法中的属性?

由于

2 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您尝试使用未初始化实例的构造函数。

// Implement your domain here using the SJS DSL.

Interface('Traceable',
interceptors: 'TraceableLifecycleInterceptor',
uncloned: ['createTimestamp',
           'lastUpdateTimestamp','lastUpdatedBy','createdBy']) {
  string_64 'createdBy',readOnly:true
  date_time 'createTimestamp', timeZoneAware: true, readOnly: true
  string_64 'lastUpdatedBy', readOnly:true
  date_time 'lastUpdateTimestamp', timeZoneAware: true, readOnly: true
}

Entity ('Project', extend:'Traceable',toString:'name',
icon:'project.png',
rendered: ['name','lastUpdateTimestamp','lastUpdatedBy','createTimestamp','createdBy'],
queryable: ['name']){
string_64 'name'
set 'technologies', composition:true, ref:'Technology'
set 'technicalTrainers', composition:true, ref:'Trainer'
set 'usesTrainers', composition:true, ref:'Trainer'
set 'students', composition:true, ref:'Student'

}

Entity ('Technology', extend:'Traceable',toString:'name',
icon:'technology.png',
rendered: ['name','lastUpdateTimestamp','lastUpdatedBy','createTimestamp','createdBy'],
queryable: ['name']){
string_64 'name'
set 'projects', ref:'Project', reverse:'Project-technologies'
set 'studentsAbleToUseIt', ref:'Technology', reverse:'Student-technologies'
set 'trainersAbleToTeachIt', ref:'Technology', reverse:'Trainer-technologies'
}

Interface ('Person', extend:'Traceable'){
string_64 'lastname'
string_64 'firstname'
date_time 'createTimestamp', timeZoneAware: true, readOnly: true
date_time 'lastUpdateTimestamp', timeZoneAware: true, readOnly: true
}

 Entity ('Trainer',
    extend: 'Person', 
    toString:'firstname',
    icon:'trainer.png',
    rendered: ['firstname','lastname','lastUpdateTimestamp','lastUpdatedBy','createTimestamp','createdBy'],
    queryable: ['firstname','lastname']){
set 'projectsAsTechnicalTrainer', ref:'Project', reverse:'Project-technicalTrainers'
set 'projectsAsUsesTrainer', ref:'Project', reverse:'Project-usesTrainers'
set 'technologies', composition:true, ref:'Technology'
date_time 'createTimestamp', timeZoneAware: true, readOnly: true
date_time 'lastUpdateTimestamp', timeZoneAware: true, readOnly: true
}

Entity ('Student',
    extend: 'Person', 
    toString:'firstname',
    icon:'student.png',
    rendered: ['firstname','lastname','lastUpdateTimestamp','lastUpdatedBy','createTimestamp','createdBy'],
    queryable: ['firstname','lastname']){
set 'technologies', composition:true, ref:'Technology'
set 'projects', ref:'Project', reverse:'Project-students'
date_time 'createTimestamp', timeZoneAware: true, readOnly: true
date_time 'lastUpdateTimestamp', timeZoneAware: true, readOnly: true
}

在这里,您将const url = new Media({ url: 'url here', }); state object属性传递给url构造函数,但实际上并没有捕获该对象来初始化您的实例,因此只是忽略该对象。因此,您的实例上的名称Media没有属性,而url将为this.url

正如文档here所述,undefined将为给定的类名返回Parse.Object.extend()的子类。 Parse.Object的第二个参数是一个包含所有实例可用的实例属性/方法的对象。定义类后,可以使用extend()关键字创建新实例。如果您在创建实例时要初始化多个参数,则可以使用下面的new方法。

initialize

然后通过调用构造函数new关键字并传递状态对象(如下面的

)来创建实例
const Media = Parse.Object.extend('Media', {
  initialize : function(state){
     this.url = state.url;
     this.type = state.type;
  },
  getUrl: function getUrl() {
    return this.url;
  },
  getType : function(){
    return this.type;
  }
});

答案 1 :(得分:0)

事实证明我尝试在嵌套对象上执行getUrl()方法,但是我没有在查询中包含此对象。