diff --git a/javascript_objects/Object/copywithin()/example1.html b/javascript_objects/Object/Object.create()/example1.html similarity index 82% rename from javascript_objects/Object/copywithin()/example1.html rename to javascript_objects/Object/Object.create()/example1.html index fe89226..b3b4df4 100644 --- a/javascript_objects/Object/copywithin()/example1.html +++ b/javascript_objects/Object/Object.create()/example1.html @@ -26,9 +26,18 @@

Write Your JavaScript Code Here

- +
- +const people = { + printIntroduction: function () + { + console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`); + } +}; +const me = Object.create(people); +me.name = "Marry"; // "name" is a property set on "me", but not on "person" +me.isHuman = true; // inherited properties can be overwritten +me.printIntroduction();
diff --git a/javascript_objects/Object/Object.create()/example2.html b/javascript_objects/Object/Object.create()/example2.html new file mode 100644 index 0000000..ddec078 --- /dev/null +++ b/javascript_objects/Object/Object.create()/example2.html @@ -0,0 +1,59 @@ + + + + + + + Example + + + + + + + + + + + + + + + +

Prototype-based Inheritance in JavaScript with Constructors using Object.create()

+
+ + +
+
+ +
+function fruits() { // Defines a constructor function named "fruits" + this.name = 'franco'; // Creates a property "name" and assigns it the value 'franco' within the "fruits" constructor +} + +function fun() { // Defines another constructor function named "fun" + fruits.call(this); // Calls the "fruits" constructor within the context of the current object (this), effectively setting its properties +} + +fun.prototype = Object.create(fruits.prototype); // Creates a new object that inherits from the prototype of "fruits" and sets it as the prototype of "fun" + +const app = new fun(); // Creates a new instance of the "fun" constructor and assigns it to the variable "app" + +console.log(app.name); // Outputs the value of the "name" property of the "app" object, which is 'franco' + +
+ +
+
+

Output:

+
+
+ + + + + + + + diff --git a/javascript_objects/Object/Object.create()/example3.html b/javascript_objects/Object/Object.create()/example3.html new file mode 100644 index 0000000..c303baa --- /dev/null +++ b/javascript_objects/Object/Object.create()/example3.html @@ -0,0 +1,61 @@ + + + + + + + Example + + + + + + + + + + + + + + + +

Prototype-based Inheritance in JavaScript using the 'create' Method

+
+ + +
+
+ +
+function fruits() { // Defines a constructor function named "fruits" + this.name = 'fruit'; // Creates a property "name" and assigns it the value 'fruit' within the "fruits" constructor + this.season = 'Winter'; // Creates a property "season" and assigns it the value 'Winter' within the "fruits" constructor +} + +function apple() { // Defines another constructor function named "apple" + fruits.call(this); // Calls the "fruits" constructor within the context of the current object (this), effectively setting its properties +} + +apple.prototype = Object.create(fruits.prototype); // Creates a new object that inherits from the prototype of "fruits" and sets it as the prototype of "apple" + +const app = new apple(); // Creates a new instance of the "apple" constructor and assigns it to the variable "app" + +console.log(app.name, app.season); // Outputs the value of the "name" and "season" properties of the "app" object, which are 'fruit' and 'Winter' respectively +console.log(app.season); // Outputs the value of the "season" property of the "app" object again, which is 'Winter' + +
+ +
+
+

Output:

+
+
+ + + + + + + + diff --git a/javascript_objects/Object/index.html b/javascript_objects/Object/index.html index 99b0162..dd46469 100644 --- a/javascript_objects/Object/index.html +++ b/javascript_objects/Object/index.html @@ -59,7 +59,7 @@ - +
@@ -67,7 +67,7 @@

Objec

This method is used to copy enumerable and own properties from a source object to a target object

Syntax - Object.assign(target, sources)

-
+
+ + + +
+
+
+

Object.create()

+

The Object.create() method is used to create a new object with the specified prototype object and properties. We can create an object without a prototype by Object.creates (null).

+

Syntax- Object.create(prototype[, propertiesObject])

+
+
+ +
+
+