<分区>
是否可以通过定义 prototype.clone() 属性为 Javascript 对象创建内联 clone() 函数?
我编写了一个名为 ZendeskUser 的简单 JavaScript 对象,需要使用 ZendeskUser.prototype 将实例函数添加到我的对象定义中(如本页底部所建议:https://developer.zendesk.com/apps/docs/agent/require)。
我正在尝试使用 ZendeskUser.prototype.clone = function() { ... } 为我的对象实例定义一个克隆函数,这样我就可以调用 myUserObject.clone() 来创建一个副本。
我可以调用它,但它正在做一些非常奇怪的事情!
代码如下:
function ZendeskOrganization( app, id, name, customer_type )
{
this.app = app;
this.id = id;
this.name = name;
this.customer_type = customer_type;
this.extra_org_fields = [];
for(var i = 0; i < app.organization_field_mappings.length; i++)
{
this.extra_org_fields[ i ] = { field_def: app.organization_field_mappings[ i ], value: null };
}
}
ZendeskOrganization.prototype.clone = function()
{
var clonedOrganization = new ZendeskOrganization( this.app, this.id, this.name, this.customer_type );
console.log( "cloning Org, this.name = '" + this.name + "', new ZendeskOrganization = ");
console.dir( clonedOrganization );
for(var i = 0; i < this.extra_org_fields.length; i++)
{
clonedOrganization.extra_org_fields[ i ] = { field_def: this.extra_org_fields[ i ].field_def, value: this.extra_org_fields[ i ].value };
}
console.log( "finished cloning Org, clonedOrganization = ");
console.dir( clonedOrganization );
return clonedOrganization;
};
function ZendeskUser(app, id, name, email, customer_type, organization_id)
{
//console.log( "Started ZendeskUser constructor with id=" + id + ", name = " + name + ", email = " + email + ", customer_type = " + customer_type + ", app = ...");
//console.dir( app );
this.app = app;
this.id = id;
this.name = name;
this.email = email;
this.customer_type = customer_type;
this.organization_id = ( typeof( organization_id ) === "undefined" ) ? null : organization_id; //this is underd to store the org id even though this info if available inside the attached org object.
this.orgObject = null; //this will only be instantiated when needed, not now, even if there is an organization id
this.extra_user_fields = [];
for(var i = 0; i < app.user_field_mappings.length; i++)
{
this.extra_user_fields[ i ] = { field_def: app.user_field_mappings[ i ], value: null };
}
}
ZendeskUser.prototype.clone = function()
{
console.log( "Started ZendeskUser.prototype.clone with this=");
console.dir( this );
console.log( "and this.orgObject = ");
console.dir( this.orgObject );
var clonedUser = new ZendeskUser( this.app, this.id, this.name, this.email, this.customer_type, this.organization_id );
clonedUser.orgObject = ( this.orgObject === null) ? null : this.orgObject.clone();
for(var i = 0; i < this.extra_user_fields.length; i++)
{
clonedUser.extra_user_fields[ i ] = { field_def: this.extra_user_fields[ i ].field_def, value: this.extra_user_fields[ i ].value };
}
console.log( "Finished ZendeskUser.prototype.clone, returning:");
console.dir( clonedUser );
return clonedUser;
};
我遇到的问题是它似乎在做非常奇怪的事情。我创建了一个新的 ZendeskUser 对象 myUser 并使用真实数据对其进行了实例化并调用了 myUser.clone() 并将其从 orgObject 属性中分离出来。
谁能理解我在 chrome Javascript 控制台中看到的输出?为什么这是一个完全设置的对象,但 this.orgObject 为空!?
忽略下面我的调试日志输出中突出显示的语法,这应该只是文本:
Started ZendeskUser.prototype.clone with this=
ZendeskUser
app: AppSubclass
customer_type: "mailshot_use_default_values"
email: "john.milner@blah.net"
extra_user_fields: Array[1]
id: 2799643595
name: "John Milner"
orgObject: ZendeskOrganization
app: AppSubclass
customer_type: "TEST_ONLY"
extra_org_fields: Array[2]
id: 3060365935
name: "My Company"
__proto__: Object
organization_id: 3060365935
__proto__: Object
and this.orgObject =
null
Finished ZendeskUser.prototype.clone, returning:
ZendeskUserapp: AppSubclass
customer_type: "mailshot_use_default_values"
email: "john.milner@blah.net"
extra_user_fields: Array[1]
id: 2799643595
name: "John Milner"
orgObject: null
organization_id: 3060365935
__proto__: Object