TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Object create() Method

JavaScript Object create() Method with example on javascript, object, javascript assign(), create(), defineProperties(), defineProperty(), entries(), freeze(), javascript tutorial, javascript array etc.

<< Back to JAVASCRIPT

JavaScript Object.create() Method

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])

Parameter

prototype: It is the prototype object from which a new object has to be created.

propertiesObject: It is an optional parameter. It specifies the enumerable properties to be added to the newly created object.

Return

Object.create() returns a new object with the specified prototype object and properties.

Browser Support:

Chrome Yes
Edge Yes
Firefox Yes
Opera Yes

Example 1

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();

Output:

"My name Marry. Am I human? true"

Example 2

function fruits() {
        this.name = 'franco';
        }
       function fun() {
        fruits.call(this)
 }

        fun.prototype = Object.create(fruits.prototype);
        const app = new fun();
        console.log(app.name);

Output:

"franco"

Example 3

function fruits() {
        this.name = 'fruit';
        this.season = 'Winter';
        }

        function apple() {
        fruits.call(this);
        }

        apple.prototype = Object.create(fruits.prototype);
        const app = new apple();
        console.log(app.name,app.season);
        console.log(app.season);

Output:

"fruit"
 "Winter"
"Winter"

Next TopicJavaScript Objects




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf