Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV-4017-prototypes-exercise-1-animals #1680

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions js/tests/animals_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
export const tests = []
const t = f => tests.push(f)

// Test Dog
t(({ eq }) => {
const Animal = {
canEat: false,
}
Object.assign(Dog.prototype, Animal)


let myDog = new Dog()
return eq(
{
redefinedCanEat: myDog.canEat,
hasOwn: Object.hasOwn(myDog, 'canEat'),
canBreath: myDog.canBreath,
hasOwn: Object.hasOwn(myDog, 'canBreath'),
isAlive: myDog.isAlive,
hasOwn: Object.hasOwn(myDog, 'isAlive'),
canRun: myDog.canRun,
hasOwn: Object.hasOwn(myDog, 'canRun'),
WhoAmI: myDog.WhoAmI(),
hasOwn: Object.hasOwn(myDog, 'WhoAmI'),
},
{
redefinedCanEat: false,
hasOwn: false,

canBreath: true,
hasOwn: false,
isAlive: true,
hasOwn: false,
canRun: true,
hasOwn: true,
WhoAmI: "I'm a dog",
hasOwn: true,
})
})

// Test Bird
t(({ eq }) => {
let myBird = new Bird()
return eq(
{
canEat: myBird.canEat,
hasOwn: Object.hasOwn(myBird, 'canEat'),
canBreath: myBird.canBreath,
hasOwn: Object.hasOwn(myBird, 'canBreath'),
isAlive: myBird.isAlive,
hasOwn: Object.hasOwn(myBird, 'isAlive'),
makesEggs: myBird.makesEggs,
hasOwn: Object.hasOwn(myBird, 'makesEggs'),
canFly: myBird.canFly,
hasOwn: Object.hasOwn(myBird, 'canFly'),
WhoAmI: myBird.WhoAmI(),
hasOwn: Object.hasOwn(myBird, 'WhoAmI'),
},
{
canEat: true,
hasOwn: false,
canBreath: true,
hasOwn: false,
isAlive: true,
hasOwn: false,
makesEggs: true,
hasOwn: true,
canFly: true,
hasOwn: true,
WhoAmI: "I'm a bird",
hasOwn: true,
})
})

// Test Dodo
t(({ eq }) => {
let myDodo = new Dodo()
return eq(
{
canEat: myDodo.canEat,
hasOwn: Object.hasOwn(myDodo, 'canEat'),
canBreath: myDodo.canBreath,
hasOwn: Object.hasOwn(myDodo, 'canBreath'),
isAlive: myDodo.isAlive,
hasOwn: Object.hasOwn(myDodo, 'isAlive'),
makesEggs: myDodo.makesEggs,
hasOwn: Object.hasOwn(myDodo, 'makesEggs'),
canFly: myDodo.canFly,
hasOwn: Object.hasOwn(myDodo, 'canFly'),
WhoAmI: myDodo.WhoAmI(),
hasOwn: Object.hasOwn(myDodo, 'WhoAmI'),
},
{
canEat: true,
hasOwn: true,
canBreath: true,
hasOwn: true,
isAlive: false,
hasOwn: true,
makesEggs: true,
hasOwn: true,
canFly: false,
hasOwn: true,
WhoAmI: "I'm a dodo",
hasOwn: true,
})
})

// Test Dodo
t(({ eq }) => {
let myDog = new Dog("Rex")
let myBird = new Bird()
let myDodo = new Dodo("Dill")
return eq(
{
name: myDog.name,
hasOwn: Object.hasOwn(myDog, 'name'),
name: myBird.name,
hasOwn: Object.hasOwn(myBird, 'name'),
name: myDodo.name,
hasOwn: Object.hasOwn(myDodo, 'name'),
},
{
name: "Rex",
hasOwn: true,
name: "Anonymous",
hasOwn: false,
name: "Dill",
hasOwn: true,
})
})

export const setup = () => {
}

Object.freeze(tests)
80 changes: 80 additions & 0 deletions subjects/animals/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
## Animals

### Instructions

Modern JavaScript implements high level tools for creating and managing classes, nevertheless most of those tools rely on low level functions still present and in use in many libraries and legacy code.

In order to understand how `prototypal inheritance` works you will have to recreate a similar behavior by using only the low level functions provided by the language.

You will have `Animal` which will an object and will have the following properties:
- `canEat`, `canBreath`, `isAlive`: All booleans, set to true.
- `name`: set to `"Anonymous"`.
- `WhoAmI`: Function that returns a string, here it will be `"I'm an animal"`.
- `NameToUppercase`: Function that returns `name` to uppercase.

The following objects will inherit from `Animal` and add/override fields as follow:
- `Dog`: adds property `canRun` set to `true`.
- `Bird`: adds properties `canFly` and `makesEggs` set to `true`.
- `Dodo`: inherits from `Bird` and overrides `canFly` and `isAlive` to `false`.

- All of them will override `WhoAmI`, returning `"I'm a [animal name]"`.
- All can accept a specific `name` as argument when created or use the default in `Animal` if no argument is provided.

> There are many different ways to work on prototypes, we suggest to use `Object.assign` and similar functions, but feel free to experiment other ways to understand the differences.

### Usage

Here is a possible program to test your function:

```javascript
// Your implementation of Animal, Dog, Bird and Dodo ...

const myDog = new Dog()
const myBird = new Bird("Trill")
const myDodo = new Dodo("Mallone")
console.log("canEat: " + myDog.canEat + " | hasOwn: " + Object.hasOwn(myDog, "canEat"))
console.log("canEat: " + myBird.canEat + " | hasOwn: " + Object.hasOwn(myBird, "canEat"))
console.log("canEat: " + myDodo.canEat + " | hasOwn: " + Object.hasOwn(myDodo, "canEat"))
console.log("canRun: " + myDog.canRun + " | hasOwn: " + Object.hasOwn(myDog, "canRun"))
console.log("canFly: " + myBird.canFly + " | hasOwn: " + Object.hasOwn(myBird, "canFly"))
console.log("canFly: " + myDodo.canFly + " | hasOwn: " + Object.hasOwn(myDodo, "canFly"))
console.log("makesEggs: " + myDodo.makesEggs + " | hasOwn: " + Object.hasOwn(myDodo, "makesEggs"))
console.log("WhoAmI: " + myDog.WhoAmI() + " | hasOwn: " + Object.hasOwn(myDog, "WhoAmI"))
console.log("WhoAmI: " + myBird.WhoAmI() + " | hasOwn: " + Object.hasOwn(myBird, "WhoAmI"))
console.log("WhoAmI: " + myDodo.WhoAmI() + " | hasOwn: " + Object.hasOwn(myDodo, "WhoAmI"))
console.log("name: " + myDog.name + " | hasOwn: " + Object.hasOwn(myDog, "name"))
console.log("name: " + myBird.name + " | hasOwn: " + Object.hasOwn(myBird, "name"))
console.log("name: " + myDodo.name + " | hasOwn: " + Object.hasOwn(myDodo, "name"))
console.log("NameToUppercase: " + myDog.NameToUppercase() + " | hasOwn: " + Object.hasOwn(myDog, "NameToUppercase"))
console.log("NameToUppercase: " + myBird.NameToUppercase() + " | hasOwn: " + Object.hasOwn(myBird, "NameToUppercase"))
console.log("NameToUppercase: " + myDodo.NameToUppercase() + " | hasOwn: " + Object.hasOwn(myDodo, "NameToUppercase"))
```
mikysett marked this conversation as resolved.
Show resolved Hide resolved

And its output :

```console
$ node animals.js
canEat: true | hasOwn: false
canEat: true | hasOwn: false
canEat: true | hasOwn: false
canRun: true | hasOwn: true
canFly: true | hasOwn: true
canFly: false | hasOwn: true
makesEggs: true | hasOwn: true
WhoAmI: I'm a dog | hasOwn: true
WhoAmI: I'm a bird | hasOwn: true
WhoAmI: I'm a dodo | hasOwn: true
name: Anonymous | hasOwn: false
name: Trill | hasOwn: true
name: Mallone | hasOwn: true
NameToUppercase: ANONYMOUS | hasOwn: false
NameToUppercase: TRILL | hasOwn: false
NameToUppercase: MALLONE | hasOwn: false
$
```

### Notions

- [Object prototypes](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes)

- [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign?retiredLocale=it)
Loading