2016-06-07 13 views

Odpowiedz

11

Object.create(proto, props) ma dwa argumenty:

  1. proto — the object which should be the prototype of the newly-created object.
  2. props (optional) — an object whose properties specify property descriptors to be added to the newly-created object, with the corresponding property names.

Format obiektu props jest zdefiniowane here.

W skrócie, dostępne opcje dla każdego deskryptora własności są takie:

{ 
    configurable: false, // or true 
    enumerable: false, // or true 
    value: undefined, // or any other value 
    writable: false, // or true 
    get: function() { /* return some value here */ }, 
    set: function (newValue) { /* set the new value of the property */ } 
} 

Problem z kodem jest to, że deskryptory własności już zdefiniowane nie są obiektami.

Oto przykład prawidłowego wykorzystania deskryptorów własności:

var test = Object.create(null, { 
    ex1: { 
     value: 1, 
     writable: true 
    }, 
    ex2: { 
     value: 2, 
     writable: true 
    }, 
    meth: { 
     get: function() { 
      return 'high'; 
     } 
    }, 
    meth1: { 
     get: function() { 
      return this.meth; 
     } 
    } 
}); 
+4

met zwraca wysoki - krzyknąłem. XD – evolutionxbox

-4

Próbowałeś tej składni:

var test = { 
 
     ex1: 1, 
 
     ex2: 2, 
 
     meth: function() { 
 
     return 10; 
 
     }, 
 
     meth1: function() { 
 
     return this.meth() 
 
     } 
 
    }; 
 

 
    console.log("test.ex1 :"+test.ex1); 
 
    console.log("test.meth() :"+test.meth()); 
 
    console.log("test.meth1() :" + test.meth1());

Powiązane problemy