2012-12-25 5 views
5

Eventuali duplicati:
Using “Object.create” instead of “new”passaggio di argomenti quando si utilizza Object.create al contrario di nuovo

Questo filo è stato chiuso perché è stato contrassegnato come duplicato, che non lo è. Il filo in questione non si concentra sul passaggio di argomenti in modo corretto quando si utilizza Object.create

Sono curioso di sapere come vorrei andare sugli oggetti di inizializzazione utilizzando Object.create al contrario di new. Ecco il mio codice finora:

function Human(eyes) { 
    this.eyes = eyes || false; 
} 

Human.prototype.hasEyes = function() { 
    return this.eyes; 
} 

function Male(name) { 
    this.name = name || "No name"; 
} 

Male.prototype = new Human(true); //passing true to the Human constructor 

var Sethen = new Male("Sethen"); 

console.log(Sethen.hasEyes()); 

Come potete vedere sopra, la Male.prototype = new Human(true); crea un nuovo oggetto con vera. Quando viene eseguita la funzione hasEyes(), questo si registra come previsto.

Quindi, la mia domanda è .. usando Object.create come dovrei andare a fare questo allo stesso modo passando un parametro true ??

+0

Dup: http://stackoverflow.com/questions/2709612/using-object-create-instead-of-new – elclanrs

+0

Questo non è un esatto duplicato tra l'altro, il filo in questione si concentra sull'utilizzo 'Object.create' su' new' dove sto cercando di concentrarmi sul passare gli argomenti con ognuno di essi, qualcosa che il thread non copre. – Sethen

+0

si potrebbe fare questo in questo modo: http://jsbin.com/umodef/1/edit – C5H8NNaO4

risposta

6

È necessario chiamare il costruttore utilizzando Object.call(this) e quindi passare i propri argomenti.

function Human(eyes, phrase) { 
    this.eyes = eyes || false; 
    this.phrase = phrase; 
} 

Human.prototype.hasEyes = function() { 
    return this.eyes; 
} 

Human.prototype.sayPhrase = function() { 
    return this.phrase; 
} 

function Male(name) { 
    Human.call(this, true, "Something to say"); //notice the call and the arguments 
    this.name = name || "No name"; 
} 

Male.prototype = Object.create(Human.prototype); 

var Sethen = new Male("Sethen"); 

console.log(Sethen.hasEyes()); 
console.log(Sethen.sayPhrase()); 

console.log(Object.getOwnPropertyNames(Sethen)); 

Questo funziona e ora l'oggetto Male ha le proprietà di eyes e phrase

+2

Grazie finalmente trovato un post che fa l'ereditarietà corretta in JavaScript :) – Xela

0

è questo dare qualche indizio su modello di ereditarietà di prototipi.

const Human = { 
    constructor: function (eyes) { 
     this.eyes = eyes || false; 
    }, 
    hasEyes: function() { 
     return this.eyes; 
    } 
}; 

let Male = Object.create(Human); 

Male.constructor = function (name) { 
    this.name = name || "No name"; 
    return Human.constructor.call(this, "true"); 
}; 

let Sethen = Object.create(Male); 
Sethen.constructor = function() { 
    return Male.constructor.call(this, 'Sethen'); 
}; 

Sethen.constructor(); 

console.log(Sethen.hasEyes()); 
console.log(Object.getOwnPropertyNames(Sethen));