javascript - 使用 prototype.clone() 的 Javascript 对象的内联 clone() 函数

标签 javascript function prototype clone

<分区>

是否可以通过定义 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

最佳答案

所以答案是肯定的,你可以添加一个克隆函数使用

ConstructorName.prototype.clone = function () { .... }; 

我的问题中的代码实际上运行良好!

我的困惑来自于 chrome 开发控制台中的 console.dir(myObjectToLog) 现在给我对象的快照,而不是 console.dir(myObjectToLog) 语句运行时对象的快照。

谢谢大家的帮助。

关于javascript - 使用 prototype.clone() 的 Javascript 对象的内联 clone() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754932/

相关文章:

javascript - 列出 Firebase 存储对象会忽略所有元数据

c - 如何找到解析形式未知的函数的根,而该函数可作为一组列表值使用?

javascript - IE 8,功能无法链接到输入按钮

javascript - 向原型(prototype)添加功能

javascript - D3 折线图中的自定义 x 轴刻度

javascript - 有条件地创建内容的 Angular Directive(指令)

javascript - 在js中找到多边形的中心点

javascript - this.apply 方法,以 this 作为参数

javascript - react : updating property of object in state

function - lua函数引用可以用作表键吗?