2016-06-07 15 views

risposta

11

Object.create(proto, props) ha due argomenti

  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.

Il formato per l'oggetto props è definita here.

In breve, le opzioni disponibili per ogni descrittore di proprietà sono queste:

{ 
    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 */ } 
} 

Il problema con il vostro codice è che i descrittori di proprietà che avete definito non sono oggetti.

Ecco un esempio di un corretto utilizzo di descrittori di proprietà:

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

meth rendimenti elevati - ho pianto. XD – evolutionxbox

-4

Hai provato la seguente sintassi:

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());