2016-10-17 8 views
0

は、私は、次のjavascriptのOO変数

var Settings = function() {   
    this.timelimit = 0; 
    this.locked = false;  
    this.expires = null;  
    this.age  = null;  
}; 

そして、私のようないくつかの取得/設定機能を設定してJavaScriptのオブジェクトを考えてみましょうdata_from_local_storageでは、上記の変数(timelimitlockedなど)に一致するJSON変数があります。

問題は、オブジェクトvar settings_ref = Settings()は、これらすべての4つの変数を持っている - だけでなく、settings_refに割り当てられたこれらの3つの機能を持っている - これによるOOの行動に私はload()関数内で記述する必要があります。

this.timelimit = data_from_local_storage.timelimit 
this.age  = data_from_local_storage.age 
this.locked = data_from_local_storage.locked 

私は this = data_from_local_storageを書くつもりならば、それは私を破壊しますのでオブジェクト。

これらの変数を1つずつ書き込むことを避けるにはどうすればよいですか?

  • は、この例では機能
  • 内部ループのOA/wを毎回
  • 私は、Pythonのようにいくつかの.update()機能を探しているだけで4ですが、はるかに多くがあると私はどこにでもそれを書くことができませんまたは何か..

誰かが知っている簡単なショートカット?

+2

self' 'へのこれらの参照は' this'でなければなりません。 – Pointy

+0

http://stackoverflow.com/questions/11197247/javascript-equivalent-of-jquerys-extend-method助けてください – Satpal

+0

あなたのすべてのオプションを別のオブジェクトリテラルの中に置きます。 'this.options = {timelimit:0、...}'を実行してから、 'this.options = data_from_local_storage'を実行するだけでオプションを非常に簡単に置き換えることができます。 。 – Keith

答えて

2

あなたはES2015にObject.assign()を使用することができます。

load: function() { 
     Object.assign(this, LoadLocalStorage()); 
    } 

それは明らかにIEにはまだサポートされていないのですが、ポリフィルon the MDN pageがあります:

if (typeof Object.assign != 'function') { 
    (function() { 
    Object.assign = function (target) { 
     'use strict'; 
     // We must check against these specific cases. 
     if (target === undefined || target === null) { 
     throw new TypeError('Cannot convert undefined or null to object'); 
     } 

     var output = Object(target); 
     for (var index = 1; index < arguments.length; index++) { 
     var source = arguments[index]; 
     if (source !== undefined && source !== null) { 
      for (var nextKey in source) { 
      if (source.hasOwnProperty(nextKey)) { 
       output[nextKey] = source[nextKey]; 
      } 
      } 
     } 
     } 
     return output; 
    }; 
    })(); 
} 

を(個人的に私は、メソッドを追加するためにObject.defineProperty()を使用することになり、しかしそれはMDNからそのまま受け継がれています。)

編集あなたは文字通り別のオブジェクト内のデータを保存する場合は、それがここに...のlocalStorageするものを持続し、戻って非常に簡単になり、Object.defineProperty()いずれか:)

+0

' Object.assign'は私が探していたもの - ありがとう! –

1

を持っていない可能性がありObject.assign()ていない例..です

//pretend local storage loader 
 
function LoadLocalStorage() { 
 
    return { 
 
     timelimit: 100, 
 
     locked: true, 
 
     expires: new Date(),  
 
     age:40 
 
    } 
 
} 
 

 
var Settings = function() {   
 
    this.data = { 
 
     timelimit: 0, 
 
     locked: false, 
 
     expires: null,  
 
     age:null 
 
    } 
 
}; 
 

 
Settings.prototype = { 
 
    getAllAges: function() { 
 
    return this.data.age; 
 
    }, 
 
    getTimeLimit: function() { 
 
    return this.data.timelimit; 
 
    }, 
 
    load: function() { 
 
    this.data = LoadLocalStorage(); 
 
    } 
 
} 
 

 
var settings = new Settings; 
 
console.log('Age before our load'); 
 
console.log(settings.getAllAges()); 
 
settings.load(); 
 
console.log('Age after our load'); 
 
console.log(settings.getAllAges());

+0

'var settings = new Settings();'として修正したいかもしれません。 – Redu

+0

@Redu明らかにオプションです。 http://stackoverflow.com/questions/3034941/new-myobject-vs-new-myobject – Keith

+0

それもオプションです、私は 'this.variable'だけ他のネイティブオブジェクトと同じようにしたい –