簡體   English   中英

如何從現有對象創建新對象?

[英]How to create a new object from an existing one?

這不起作用,我也不知道如何解決它

function bar() {...}

function foo() {
    this = new bar();

    this.newfunction = function() {...};
    this.newvalue = "foobar";
}

var foobar = new foo();

提前致謝,

不要使用this來表示另一個對象。

function bar() {...}

function foo() {
    var bar = new bar();

    bar.newfunction = function() {...};
    bar.newvalue = "foobar";
}

var foobar = new foo();

您是否要從bar繼承? 然后,您可以使用call (或apply )借用其構造函數及其所有屬性:

function bar() {...}

function foo() {
    bar.call(this);
    this.newfunction = function() {...};
    this.newvalue = "foobar";
}

var foobar = new foo();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM