let's say that I've got a parent (let's say base) object, and a child object that's an extended version of base one.
I'll use sample code from metaduck.com. Here we have the parent:
function Animal() {
var walked = false
function walk() {
console.log('walking...')
walked = true
}
function hasBeenWalked() {
return walked
}
return {
walk: walk
, hasBeenWalked: hasBeenWalked
}
}
module.exports = Animal
and the child:
var Animal = require('./animall')
function Cat() {
var cat = {}
var walked = false
cat.__proto__ = Animal()
cat.pat = function pat() {
console.log('being patted')
}
cat.lasagna = function lasagna() {
console.log('Lasagna!')
walked = true
}
return cat
}
module.exports = Cat
It works great if you don't have any parameter in constructors, but let's say that we've an Animal(color,size)
and a Cat(name)
.
Is there a simple way to get a Cat('Garfield')
filled with data from already existing Animal('orange','big')
? I would like to make some kind of factory from Animal that creates a lot of Cats with different names. How can I do that?
Aucun commentaire:
Enregistrer un commentaire