Skip to content

4.15 Prototypes Part 4

Object.create(proto)

Task 1: Create book objects using a book constructor. Hint: use the book object below.

/*
var book = {
  weight: null,
  pages: null
};
*/

function Book(w, p){
  this.weight = w;
  this.pages = p;
}

var book1 = new Book(0.5, 500);
var book2 = new Book(0.2, 200);
var book3 = new Book(0.7, 1000);

console.log(book1);
console.log(book2);
console.log(book3);

The result is as follows:

Book {weight: 0.5, pages: 500}
Book {weight: 0.2, pages: 200}
Book {weight: 0.7, pages: 1000}

Task 2: Can you create book objects using the below book object. Hint: use Object.create(book)

var book = {
  weight: null,
  pages: null
};

var book1 = Object.create(book);

console.log(book1);
console.log(book1.weight);
console.log(book1.pages);

The result is as follows:

{}
null
null

Task 3: Refer to task 2; add title property to book1 object. The title value is "The Four Agreements: A Practical Guide to Personal Freedom".

var book = {
  weight: null,
  pages: null
};

var book1 = Object.create(book);
book1.title = "The Four Agreements: A Practical Guide to Personal Freedom";

console.log(book1.title);

The result is: The Four Agreements: A Practical Guide to Personal Freedom

Task 4: Refer to task 3; do book and book1 objects have the property title?

var book = {
  weight: null,
  pages: null
};

var book1 = Object.create(book);
book1.title = "The Four Agreements: A Practical Guide to Personal Freedom";

console.log(`title is book property: ${book.hasOwnProperty("title")}`);
console.log(`title is book1 property: ${book1.hasOwnProperty("title")}`);

The result is as follows:

title is book property: false
title is book1 property: true

Task 5: Refer to task 2; create the book objects below using Object.create().

/*
var book1 = {weight:0.5, pages:500};
var book2 = {weight:0.2, pages:200};
var book3 = {weight:0.7, pages:1000};
*/

var book = {
  weight: null,
  pages: null
};

var book1 = Object.create(book);
book1.weight = 0.5;
book1.pages = 500;

var book2 = Object.create(book);
book2.weight = 0.2;
book2.pages = 200;

var book3 = Object.create(book);
book3.weight = 0.7;
book3.pages = 1000;

console.log(book1);
console.log(book2);
console.log(book3);

The result is as follows:

{weight: 0.5, pages: 500}
{weight: 0.2, pages: 200}
{weight: 0.7, pages: 1000}

Task 6: Refer to task 5; What is the prototype of book1 object? Hint: use Object.getPrototypeOf(book1).

var book = {
  weight: null,
  pages: null
};

var book1 = Object.create(book);
book1.weight = 0.5;
book1.pages = 500;

console.log(Object.getPrototypeOf(book1));

The result is: {weight: null, pages: null}

Task 7: Refer to task 5; What is the prototype of book2 object?

var book = {
  weight: null,
  pages: null
};


var book2 = Object.create(book);
book2.weight = 0.2;
book2.pages = 200;


console.log(Object.getPrototypeOf(book2));

The result is: {weight: null, pages: null}

Task 8: Refer to task 5; What is the prototype of book3 object?

var book = {
  weight: null,
  pages: null
};

var book3 = Object.create(book);
book3.weight = 0.2;
book3.pages = 200;

console.log(Object.getPrototypeOf(book3));

The result is: {weight: null, pages: null}

Object.create()

You can create an object from an existing object using newObj = Object.create(obj1). obj1 will be a prototype for the newly created object.

newObj inherits the properties and methods from obj1. You can also add new properties and methods to newObj


Object.create(proto, properties)

Task 9: Create an object with value: 5 key-value pair.

var obj1 = {
  value: 5
};

Task 10: Create an object with value: 10 key-value pair.

var obj2 = {
  value: 10
};

Task 11: Refer to task 9 and task 10; create an object with prop1:obj1, prop2:obj2 key-value pairs.

var obj = {
  prop1: {
    value: 5
  },
  prop2:{
    value:10
  }
};

console.log(obj);

The result is: {prop1: {…}, prop2: {…}}

Task 12: Refer to task 11; create a new object with obj properties. Hint: use Object.create({}, obj).

var obj = {
  prop1: {
    value: 5
  },
  prop2:{
    value:10
  }
};

var myObj = Object.create({}, obj);

console.log(myObj);

The result is: {prop1: 5, prop2: 10}

Task 13: Create an object using Object.create() with the properties below.

/*
Object properties:
os: "Android"
ram: "8GB"
storage: "512GB"
*/

var phone = Object.create({}, {
  os: {
    value: "Android"
  },
  ram: {
    value: "8GB"
  },
  storage: {
    value: "512GB"
  }
  });

console.log(phone);

The result is: {os: "Android", ram: "8GB", storage: "512GB"}

Task 14: Create an object using Object.create() with the properties below.

/*
Object Properties:
firstName: "Ali"
lastName: "Zaki"
username: "AliZaki88"
email: "alizaki@mail.com"
*/

var user = Object.create({},{
  firstname: {
    value: "Ali"
  },
  lastName: {
    value: "Zaki"
  },
  username: {
    value: "AliZaki88"
  },
  email: {
    value: "alizaki@mail.com"
  }
  });

console.log(user);

The result is: {firstname: "Ali", lastName: "Zaki", username: "AliZaki88", email: "alizaki@mail.com"}

Task 15: Create an object using Object.create() with the properties below.

/*
Object Properties:
title: "Orobroy"
artist: "Dorantes"
album: "PASSO A DUE"
*/

var song = Object.create({}, {
  title: {
    value: "Orobroy"
  },
  artist: {
    value: "Dorantes"
  },
  album: {
    value: "PASSO A DUE"
  }
  });

console.log(song);

The result is : {title: "Orobroy", artist: "Dorantes", album: "PASSO A DUE"}

Task 16: Create an object using Object.create() with the properties below.

/*
Object Properties:
name: "Mahabharata"
author: "Vyasa"
language: "Sanskrit"
verses: 200000
summary: "It narrates the struggle between two groups of cousins in the Kurukshetra War and the fates of the Kaurava and the Pāṇḍava princes and their successors."
}
*/

var epic = Object.create({}, {
  name: {
    value: "Mahabharata"
  },
  author: {
    value: "Vyasa"
  },
  language: {
    value: "Sanskrit"
  },
  verses:{
    value: 200000
  },
  summary:{
    value: "It narrates the struggle between two groups of cousins in the Kurukshetra War and the fates of the Kaurava and the Pāṇḍava princes and their successors."
  }
  });

console.log(epic);

The result is: {name: "Mahabharata", author: "Vyasa", language: "Sanskrit", verses: 200000, summary: "It narrates the struggle between two groups of cou…ava and the Pāṇḍava princes and their successors."}

Task 17: Create an object using Object.create() with the properties below.

/*
Object Properties:
name: "Python"
filenameExtension: ".py"
stableRelease: "3.8.5"
*/

var lang = Object.create({}, {
  name:{
    value: "Python"
  },
  filenameExtension: {
    value: ".py"
  },
  stableRelease: {
    value: "3.8.5"
  }
  });

console.log(lang);

The result is: {name: "Python", filenameExtension: ".py", stableRelease: "3.8.5"}

Task 18: Create a new object from the book object below. Set the book weight value to 0.2, and add author: "Nawras Ali" key-value pair to the newly created object. Use Object.create().

var book = {
  weight: null,
  pages: null
};

var newBook = Object.create(book, {
  weight:{
    value: 0.2
  },
  author: {
    value: "Nawras Ali"
  }
  });

console.log(newBook);

The result is: {weight: 0.2, author: "Nawras Ali"}

Task 19: Create a new object from the food object below. Set the food group value to "vegetables", and add name: "Leafy green" key-value pair to the newly created object. Use Object.create().

var food = {
  group: null
};

var myFood = Object.create(food, {
  group:{
    value: "vegetables"
  },
  name: {
    value: "Leafy green"
  }
  });

console.log(myFood);

The result is: {group: "vegetables", name: "Leafy green"}

Task 20: Create a new object from the song object below. Set the song genre value to "Jazz", and add artist: "Miles Davis" key-value pair to the newly created object. Use Object.create().

var song = {
  genre: null,
  origin: null,
  title: null
};

var mySong = Object.create(song, {
  genre:{
    value: "Jazz"
  },
  artist: {
    value: "Miles Davis"
  }
  });

console.log(mySong);

The result is: {genre: "Jazz", artist: "Miles Davis"}

Object.create(proto, properties)

You can add key-value pairs to the newly created object with adding a new parameter to Object.create().

The properties are added or modified in the following way:

{ prop1: { value: ..., }, prop2: { value: ...., } .... }


Object.getPrototypeOf()

Task 21: What is the prototype of the object below. Hint: use Object.getPrototypeOf(obj).

var obj = {
  item1: 1,
  item2: 2,
  item3: 3
}

console.log(Object.getPrototypeOf(obj));

The result is as follows:

{constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}

Task 22: What is the prototype of the user object below. Hint: use Object.getPrototypeOf(obj).

function User(fstName, lstName){
  this.firstName = fstName;
  this.lastName = lstName;
}

var user1 = new User("Nawras", "Ali");

console.log(Object.getPrototypeOf(user1));

The result is as follows:

{constructor: ƒ}

Task 23: What is the prototype of the object below.

var obj = new Array();

console.log(Object.getPrototypeOf(obj));

The result is: [constructor: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, find: ƒ, …]

Task 24: What is the prototype of the object below.

var user = {
  firstName: "Ali",
  lastName: "Zaki",
  username: "AliZaki88",
  email: "alizaki@mail.com"
}

console.log(Object.getPrototypeOf(user));

The result is: {constructor: ƒ, defineGetter: ƒ, defineSetter: ƒ, hasOwnProperty: ƒ, lookupGetter: ƒ, …}

Task 25: What is the prototype of the object below.

var func = new Function()

console.log(Object.getPrototypeOf(func));

The result is: ƒ () { [native code] }

getPrototypeOf

Object.getPrototypeOf returns the prototype of the specified object.

__proto__ VS getPrototypeOf

Both __proto__ and getPrototypeOf access the prototype of an object. However, you can not modify the prototype of an object using getPrototypeOf, but you can using __proto__.

__proto__ is now deprecated