亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

克隆包含函數的 javascript 對象

克隆包含函數的 javascript 對象

拉丁的傳說 2022-10-21 10:18:46
我正在編寫一個具有不同形狀的 javascript 圖形游戲,一種猜測形狀。每個都是較小形狀的不同組合。shapemodel 構造函數是具有 3 種不同方法的顏色,所有函數調用各種繪圖代碼。我已經復制了列表的創建方式。    var shapeList = [];    var shapeArray = [            new shapeModel("#ff0", FShape1, circleSegment, miscShape1),            new shapeModel("#f00", FShape1, circleHollow, miscShape1),            new shapeModel("#000", FShape1, circleSegment, miscShape2),            new shapeModel("#08f", FShape2, circleSegment, miscShape1),            new shapeModel("#060", FShape2, circleHollow, miscShape1),            new shapeModel("#007", FShape2, circleSegment, miscShape2),            new shapeModel("#0f7", FShape1, circleHollow, miscShape2),            new shapeModel("#888", FShape2, circleHollow, miscShape2)        ];        shapeArray.forEach(function (value, index) {        shapeList.push(value);    });        function shapeModel(c, fshape, circle, misc) {            var newElement = {            shapeColour: c,            startX: 200,            startY: 200,            thickness: 6,            fullShape: function () {                fshape(this);                circle(this);                misc(this);            }        }            return newElement;    }我的問題是通過值而不是引用將整個列表復制到一個新列表,然后隨機選擇 8 個較大形狀中的 1 個,而不會干擾原始列表。var tempList = shapeList.slice(0);不創建獨立列表但至少復制所有元素var tempList = JSON.parse(JSON.stringify(shapeList));不復制函數并將它們保留為“未定義”。可能有無數的解析和字符串破解,但它變得非常混亂。我真的可以用函數來做到這一點,還是我需要重新考慮整個結構?
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

在這里解決這個問題的最簡單方法如下:

轉換shapeModel為類?;蛘吒咏惶谜n。它已經是你調用的構造函數new,所以你不妨使用原型繼承,讓你的生活更輕松。

  1. ShapeModel用大寫字母命名S。這是構造函數的公認約定。

  2. 分配您獲得的所有構造函數參數this,以便您以后可以重新使用它們。

  3. fullShape方法移至原型。作為一個優勢,您不需要為您創建fullShape的每一個函數提供一個函數ShapeModel- 內存中只有一個函數,并且所有ShapeModels 都共享它。如果你有很多這些,它會減少內存占用。

  4. 添加一個.clone()方法,以便您可以從舊實例創建新實例。通過這種方式,很容易維護如何克隆東西的邏輯,并且如果你真正需要的只是克隆一種類型的對象,你不需要想出一個很難適應的超級通用克隆機制。

一旦完成,您就有了一個簡單且可重用的構造函數。由于您可以獲取任何對象并調用.clone()它,因此制作副本的方法很簡單:

shapeArray.map(function(model) {

  return model.clone();

});

這是整個事情的樣子:


function ShapeModel(c, fshape, circle, misc) {

  this.shapeColour = c;

  this.fshape = fshape;

  this.circle = circle;

  this.misc = misc;


  this.startX = 200;

  this.startY = 200;

  this.thickness = 6;


  return newElement;

}


ShapeModel.prototype.fullShape = function() {

  this.fshape(this);

  this.circle(this);

  this.misc(this);

}


ShapeModel.prototype.clone = function() {

  return new ShapeModel(this.shapeColour, this.fshape, this.circle, this.misc);

}


var shapeArray = [

  new ShapeModel("#ff0", FShape1, circleSegment, miscShape1),

  new ShapeModel("#f00", FShape1, circleHollow, miscShape1),

  new ShapeModel("#000", FShape1, circleSegment, miscShape2),

  new ShapeModel("#08f", FShape2, circleSegment, miscShape1),

  new ShapeModel("#060", FShape2, circleHollow, miscShape1),

  new ShapeModel("#007", FShape2, circleSegment, miscShape2),

  new ShapeModel("#0f7", FShape1, circleHollow, miscShape2),

  new ShapeModel("#888", FShape2, circleHollow, miscShape2)

];



var shapeList = shapeArray.map(function(model) {

  return model.clone;

});


查看完整回答
反對 回復 2022-10-21
  • 1 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號